有两个列,一个No列,一个数据列。按照No列进行排序,但这个列的值是可能重复的,比如
No    数据
1      111
1      112
2      222
2      223
3      333
4      444请问用什么数据结构可以支持重复的key排序。

解决方案 »

  1.   

    是按No排序,No相同再按数据排序吗? 这就是按多个关键字排序.
    下面用自定义排序,
    package yangyang.com;import java.util.Comparator;
    import java.util.ArrayList;
    import java.util.Collections;
    class PrimaryKey {
     int key1;  int key2; PrimaryKey(int key1, int key2) {
    this.key1 = key1;
    this.key2 = key2;
    } public String toString() {
    return "(" + this.key1 + "," + this.key2 + ")";
    }
    }
    class Comp implements Comparator {
    public int compare(Object o1, Object o2) {
    PrimaryKey p1 = (PrimaryKey) o1;
    PrimaryKey p2 = (PrimaryKey) o2;
    if (p1.key1 < p2.key1)
    return 0;
    else if (p1.key1 > p2.key1)
    return 1;
    else if (p1.key2 <= p2.key2)
    return 0;
    else
    return 1;
    }
    }public class Check {

    /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    ArrayList list = new ArrayList<PrimaryKey>();
    list.add(new PrimaryKey(1, 111));
    list.add(new PrimaryKey(1, 112));
    list.add(new PrimaryKey(3, 333));
    list.add(new PrimaryKey(3, 339));
    list.add(new PrimaryKey(2, 222));
    Comparator comp = new Comp();
    Collections.sort(list, comp);
    for (int i = 0; i < list.size(); i++) {
    PrimaryKey p = (PrimaryKey) list.get(i);
    System.out.println(p);
    }
    }
    }
      

  2.   

    直接使用TreeMap
    自动按照key来排序