同时根据两个参数进行集合排序

发布于 2024-10-30 20:27:05 字数 856 浏览 1 评论 0原文

我有一个带有两个日期字段的类,说:

class TestData {
    Date activation;
    Date timeStamp;
}

我想根据激活日期对上述类的列表进行排序,如果它们相等,则根据时间戳进行排序,即最大值(激活)和最大值(时间戳)。

我尝试的代码如下,仅获取 max(activation)

public class CollectionSort {

    public static void main(String[] args) {
        List<TestData> testList = new ArrayList<TestData>();

        Collections.sort(testList, new Comparator<TestData>() {

            @Override
            public int compare(TestData t1, TestData t2) {
                int result = 0;
                if (t1.getActivation().before(t2.getActivation())) {
                        result = 1;
                }
                return result;
            }
        });
        System.out.println("First object is " + testList.get(0));
    }
}

任何帮助将不胜感激。

谢谢

I have a class with two date fields say:

class TestData {
    Date activation;
    Date timeStamp;
}

I want to sort the list of the above class on basis of activation date and if they are equal then on basis of timestamp i.e. max(activation) and max(timeStamp).

Code I tried is as follws which only fetch max(activation)

public class CollectionSort {

    public static void main(String[] args) {
        List<TestData> testList = new ArrayList<TestData>();

        Collections.sort(testList, new Comparator<TestData>() {

            @Override
            public int compare(TestData t1, TestData t2) {
                int result = 0;
                if (t1.getActivation().before(t2.getActivation())) {
                        result = 1;
                }
                return result;
            }
        });
        System.out.println("First object is " + testList.get(0));
    }
}

Any help would be greatly appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青衫负雪 2024-11-06 20:27:06

以下是如何在纯 Java 中执行此操作:

 public int compare(TestData o1, TestData o2) {
    int result = o1.getActivation().compareTo(o2.getActivation()));
    if(result==0) result = o1.getTimeStamp().compareTo(o2.getTimeStamp());
    return result;
 }

或者使用 Guava (使用 ComparisonChain):

public int compare(TestData o1, TestData o2) {
    return ComparisonChain.start()
      .compare(o1.getActivation(), o2.getActivation())
      .compare(o1.getTimeStamp(), o2.getTimeStamp())
      .result();
 }

或者使用 Commons / Lang (使用 CompareToBuilder) :(

public int compare(TestData o1, TestData o2) {
    return new CompareToBuilder()
      .append(o1.getActivation(), o2.getActivation())
      .append(o1.getTimeStamp(), o2.getTimeStamp())
      .toComparison();
 }

所有三个版本都是等效的,但纯 Java 版本是最冗长的,因此最容易出错。所有三个解决方案都假设 o1.getActivation()o1.getTimestamp () 实现Comparable)。

Here's how to do it in Plain Java:

 public int compare(TestData o1, TestData o2) {
    int result = o1.getActivation().compareTo(o2.getActivation()));
    if(result==0) result = o1.getTimeStamp().compareTo(o2.getTimeStamp());
    return result;
 }

Or with Guava (using ComparisonChain):

public int compare(TestData o1, TestData o2) {
    return ComparisonChain.start()
      .compare(o1.getActivation(), o2.getActivation())
      .compare(o1.getTimeStamp(), o2.getTimeStamp())
      .result();
 }

Or with Commons / Lang (using CompareToBuilder):

public int compare(TestData o1, TestData o2) {
    return new CompareToBuilder()
      .append(o1.getActivation(), o2.getActivation())
      .append(o1.getTimeStamp(), o2.getTimeStamp())
      .toComparison();
 }

(All three versions are equivalent, but the plain Java version is the most verbose and hence most error-prone one. All three solutions assume that both o1.getActivation() and o1.getTimestamp() implement Comparable).

风追烟花雨 2024-11-06 20:27:05

这样就可以了。!

        Collections.sort(yourList, new Comparator<TestData>() {    
            public int compare(TestData o1, TestData o2) {
                int date1Diff = o1.getActivation().compareTo(o2.getActivation());
                return date1Diff == 0 ? 
                        o1.geTimestamp().compareTo(o2.getTimestamp()) :
                        date1Diff;
            }               
        });

This would do it.!

        Collections.sort(yourList, new Comparator<TestData>() {    
            public int compare(TestData o1, TestData o2) {
                int date1Diff = o1.getActivation().compareTo(o2.getActivation());
                return date1Diff == 0 ? 
                        o1.geTimestamp().compareTo(o2.getTimestamp()) :
                        date1Diff;
            }               
        });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文