有一堆字符串,类似 "1,A01","3,D05","1,B02","2,A02",我现在想让他排成 "1,A01","1,B02","2,A02","3,D05",就是先按字符串里前面的数字排序,然后再按后面的字符串排序。。请解答。。感谢。。
最好提供代码

解决方案 »

  1.   

    其实那字符串后面还有其他的字符,"1,A01,lav。。"
      

  2.   

    用compareTo直接比较就能排序了
      

  3.   

    其实这个问题和一般的排序没有什么区别的。可以采用冒泡排序,只是比较的时候特殊了一点。比较两个string的时候可以采用split()方法得到string[] {数字,字母组合},先比较数字,假如数字相等比较字母组合。 时间复杂度还是O(n*lgn)
      

  4.   

    参考http://topic.csdn.net/u/20100429/12/7384d296-c015-4868-b5b0-c5bc4af8cc15.html
      

  5.   

    有规律的字符串,直接用Arrays.sort()排序,内部里用到了Comparator和compareTo
    for example
    String a[] = {"1,A01","3,D05","1,B02","2,A02"};
    Arrays.sort(a, new Comprator<String> {
        int compare(String s1, String s2) {
            if (s1==null) { return (s2==null ? 0 : -1); }
            return s1.compareTo(s2);
        }
    });
      

  6.   

    String[] a7 = {"1,A01","3,D05","1,B02","2,A02"};
    Arrays.sort(a7);
    for (String str : a7) {
    System.out.println(str);
    }
      

  7.   

    去试试Apache-commons-lang里的类和方法
    对于String类有很多好玩的方法
    俺没时间去试验,LZ有兴趣可以去看看
      

  8.   

    实验了一下,以下是代码:package PaixuDuoZifu;import java.util.*;public class PaixuDuoZifu {

    public void paixu(String[] str){
    Comparator<String> cp = new Comparator<String>(){
    public int compare(String str1, String str2){
    String s1 = str1.split(",")[0];
    String s2 = str2.split(",")[0];
    int it1 = Integer.parseInt(s1);
    int it2 = Integer.parseInt(s2);
    if(it1>it2){
    return 1;
    }
    else if(it1<it2){
    return -1;
    }

    String s3 = str1.split(",")[1];
    String s5 = str2.split(",")[1];
    int com = s3.compareTo(s5);
    return com;

    }
    };

    Arrays.sort(str, cp);
    for(String string : str){
    System.out.print(string+"\t");
    }
    }

    public static void main(String[] args){
    PaixuDuoZifu pxzf = new PaixuDuoZifu();
        String[] str = new String[]{"1,A01","3,D05","1,B02","2,C03","2,A02","3,A06"};
    pxzf.paixu(str);
    }
    }以下是结果输出:
    1,A01 1,B02 2,A02 2,C03 3,A06 3,D05
      

  9.   

    我把你的题改动一下,免得大家误解。有一堆字符串,类似 "1,A01","13,D05","1,B02","2,A02",我现在想让他排成 "1,A01","1,B02","2,A02","13,D05",就是先按字符串里前面的数字排序,然后再按后面的字符串排序。。
      

  10.   


    /**
     * 自定义字符串类
     * http://www.juheit.com
     */ 
    class CustomString implements Comparable{//Comparable<CustomString>{

    /**
     * 字符串单元
     */
    String content;

    public CustomString(){

    }

    public CustomString(String content){
    this.content = content;
    }

    public String getContent(){
    return this.content;
    }

    /**
     * 实现接口中的compareTo方法
     */
    public int compareTo(Object obj){//(CustomString obj){ if(obj == null || !(obj instanceof CustomString)){
    return -1;
    }

    CustomString cs = (CustomString)obj;

    if(Integer.parseInt(this.getContent().substring(0, this.getContent().indexOf(","))) != Integer.parseInt(cs.getContent().substring(0, cs.getContent().indexOf(",")))){
    return Integer.parseInt(this.getContent().substring(0, this.getContent().indexOf(","))) - Integer.parseInt(cs.getContent().substring(0, cs.getContent().indexOf(",")));
    }else{
    return this.getContent().substring(this.getContent().indexOf(",") + 1).compareTo(cs.getContent().substring(cs.getContent().indexOf(",") + 1));
    }
    }

    然后将"1,A01"作为content属性赋值给CustomString,将CustomString对象装到List或Array中,调用Collections.sort或Arrays.sort的方法就可以了。
      

  11.   


    sb,算错了都不懂,还冒充高手来改题目?sb中的sb
      

  12.   

    可以使用TreeSet,更改比较模式