java排序问题

时间:2012-03-19 08:40:10

标签: java sorting

我有一个POJO的arraylist,其中的数据是

的形式
id    time
2     467
3     403
4     602
3     529
5     398

要求是首先我需要按时间对数据进行排序,然后相同的ID应该是一个接着一个,即

id     time
5      398
3      403
3      529
2      467
4      602.

最初按时间排序,我使用以下逻辑

Collections.sort(list, new Comparator<Asset>() {
                    @Override
                    public int compare(Asset o1, Asset o2) {

                        if (o1.getTime() > o2.getTime())

                            return -1;

                        else if (o1.getTime() < o2.getTime())

                            return 1;

                        else

                            return 0;

                    }

                });

在下一阶段,有人可以通过身份证帮助我吗?

3 个答案:

答案 0 :(得分:2)

要根据您提供的示例对数据进行排序,您可能需要在列表上进行两次传递。 (您如何判断3 504之前或之后5 315是否应该出现?)

  1. 按时间排序。
  2. 根据每个id的第一个索引对列表进行排序。
  3. 以下是一些示例代码:

    import java.util.*;
    
    class Asset {
        public int id;
        public int time;
    
        public Asset(int id, int time) {
            this.id = id;
            this.time = time;
        }
    
        public String toString() {
            return id + "  " + time;
        }
    }
    
    
    class Test {
        public static void main(String[] args) {
    
            List<Asset> assets = new ArrayList<Asset>();
            assets.add(new Asset(2, 467));
            assets.add(new Asset(3, 403));
            assets.add(new Asset(4, 602));
            assets.add(new Asset(3, 529));
            assets.add(new Asset(5, 398));
    
            // Sort according to time.
            Collections.sort(assets, new Comparator<Asset>() {
                @Override
                public int compare(Asset o1, Asset o2) {
                    return new Integer(o1.time).compareTo(o2.time);
                }
            });
    
            // Remember the original indexes of each asset.
            final List<Asset> assetsCopy = new ArrayList<Asset>(assets);
    
            // Sort the collection based on the index of the first asset
            // with the same id
            Collections.sort(assets, new Comparator<Asset>() {
    
                private int firstIndexOf(int id) {
                    for (int i = 0; i < assetsCopy.size(); i++)
                        if (assetsCopy.get(i).id == id)
                            return i;
                    return -1;
                }
    
                @Override
                public int compare(Asset o1, Asset o2) {
                    return new Integer(firstIndexOf(o1.id))
                            .compareTo(firstIndexOf(o2.id));
                }
            });
    
    
            for (Asset a : assets)
                System.out.println(a);
        }
    }
    

    <强>输出:

    5  398
    3  403
    3  529
    2  467
    4  602
    

答案 1 :(得分:0)

下面的代码就是一个例子。我们的想法是检查时间是否相等,是否按ID排序,否则按时排序。

Collections.sort(list, new Comparator<Asset>() {
     @Override
     public int compare(Asset o1, Asset o2) {
         if(o1.getTime() != 02.getTime()) {
             if (o1.getTime() > o2.getTime())
                 return -1;
             else
                 return 1;
         } else {
             return new Integer(o1.getId()).compareTo(o2.getId());
         }
     });

答案 2 :(得分:0)

由于Collections.sort(...)保证是稳定排序算法,您可以先按时间排序,然后按ID排序。

稳定排序保证相同的元素保持与以前相同的顺序。

对于你的例子:

  1. 按时间排序
  2. 按ID排序
  3. 在第一步之后,所有时间都会被排序。在第二步之后,您的集合按id排序 - 对于相同的ID,它们保持相同的顺序,因为排序是稳定的。

    因此,对于相同的ID,它们仍然按时间排序在第二位。 您可以根据需要将其驱动到尽可能多的排序,而无需保留任何先前排序状态。这只是因为你正在使用稳定的排序。

    <强>输出

    2  467
    3  403
    3  529
    4  602
    5  398