数据库有一列的数据为
A01,A02-A11
B01,B02-B11
C01,C02……我现在要把它按照我的规则排序。
我要 A01,B01,C01… 然后再A02,B02

解决方案 »

  1.   

    一个正则  把你的key 分成2块,英文一块,数字一块,然后自定义排序,优先按照 数字排,然后再按照英文排  就好了~
      

  2.   

    按你说的做的,没考虑其他的,试试public class Test1 {
    public static void main(String[] args) {
    String[] strArray = new String[33];
    String[] prefix = {"A", "B", "C"};
    for(int i = 0 ; i < strArray.length; i ++){
    strArray[i] = prefix[i / 11];
    int value = i % 11 + 1;
    if(value < 10)
    strArray[i] += "0";
    strArray[i] += value;
    }
    Arrays.sort(strArray, new CustomizeComparator());
    for(String str : strArray){
    System.out.println(str);
    }
    }

    static class CustomizeComparator implements Comparator<String>{
    @Override
    public int compare(String o1, String o2) {
    int value = o1.substring(1).compareTo(o2.substring(1)); 
    if(value != 0)
    return value;
    return o1.compareTo(o2);

    }
    }
    }
      

  3.   

    谢谢各位了,我用数据库查询语句给弄出来了。谢谢大家的帮忙。
    在这里贴上来 以防有同样烦恼的人遇到。
    Sql语句:
    select d.sample_num,
           d.deat_well,
           subStr(d.deat_well, 1, 1) as oneWell,
           subStr(d.deat_well, 2) as twoWell
      from ckb_distribution d
     order by twoWell, oneWell

      

  4.   


            public int compare(String o1, String o2) {
                int value = o1.substring(1).compareTo(o2.substring(1)); 
    //第一部分(A,B,C...)
                if(value != 0)
                    return value;
    //第二部分(01,02,03...)
                return o1.compareTo(o2);
                 
            }
      

  5.   

    Collections.sort(list,new Comparator(){
       ...
    });具体查API文档
      

  6.   

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    public class Hello {
    public static void main(String[] args) {
    // A01,A02-A11
    // B01,B02-B11
    // C01,C02……
    List<String> list = new ArrayList<String>();

    // Initial the list
    for (int i = 0; i < 3; ++i) {
    char ch = (char) ('A' + i);

    for (int j = 1; j <= 11; ++j) {
    list.add("" + ch + ((j < 10) ? ("0" + j) : j));
    }
    }

    // Display the original list's content
    System.out.println(list);

    // Sort the list using a specified comparator
    Collections.sort(list, new Comparator<String>() {
    @Override
    public int compare(String a, String b) {
    char chA = a.charAt(0);
    char chB = b.charAt(0);
    int numA = Integer.parseInt(a.substring(1));
    int numB = Integer.parseInt(b.substring(1));

    if (numA < numB) {
    return -1;
    } else if (numA > numB) {
    return 1;
    } else {
    return chA - chB;
    }
    }
    });

    // Display the list's content after sorting
    System.out.println(list);
    }
    }[A01, A02, A03, A04, A05, A06, A07, A08, A09, A10, A11, B01, B02, B03, B04, B05, B06, B07, B08, B09, B10, B11, C01, C02, C03, C04, C05, C06, C07, C08, C09, C10, C11]
    [A01, B01, C01, A02, B02, C02, A03, B03, C03, A04, B04, C04, A05, B05, C05, A06, B06, C06, A07, B07, C07, A08, B08, C08, A09, B09, C09, A10, B10, C10, A11, B11, C11]