日期排序 - 从最新到最旧

发布于 2024-08-28 05:44:29 字数 330 浏览 7 评论 0原文

Collections.sort(someList, new Comparator<SomeObject>() {
            public int compare(final SomeObject object1, final SomeObject object2) {
                return (object1.getSomeDate()).compareTo(object2.getSomeDate()); 
            }}
        );

它会给我具有最新日期的对象,这意味着列表将包含具有最新日期到最旧日期的对象集吗?

Collections.sort(someList, new Comparator<SomeObject>() {
            public int compare(final SomeObject object1, final SomeObject object2) {
                return (object1.getSomeDate()).compareTo(object2.getSomeDate()); 
            }}
        );

Would it give me the objects with latest dates meaning the list will contain the set of objects with latest date to oldest date?

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

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

发布评论

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

评论(6

橘亓 2024-09-04 05:44:29

Comparator.comparing

您可以传递方法引用< /a> 到 比较器.比较

如果您希望对象根据日期按升序排序:

someList.sort(Comparator.comparing(SomeObject::getSomeDate));

someList.sort(Comparator.comparing(SomeObject::getSomeDate).reversed());

按降序排序。

Comparator.comparing

You can pass a method reference to Comparator.comparing.

If you want the objects to be sorted in ascending order based on the date:

someList.sort(Comparator.comparing(SomeObject::getSomeDate));

or

someList.sort(Comparator.comparing(SomeObject::getSomeDate).reversed());

for descending order.

淑女气质 2024-09-04 05:44:29

确保您可以使用:

Collections.sort(someList, new Comparator<SomeObject>() {
        public int compare(final SomeObject object1, final SomeObject object2) {
            return object1.getSomeDate().after(object2.getSomeDate()) ? 1 : -1; 
        }}
);

To be sure you can use:

Collections.sort(someList, new Comparator<SomeObject>() {
        public int compare(final SomeObject object1, final SomeObject object2) {
            return object1.getSomeDate().after(object2.getSomeDate()) ? 1 : -1; 
        }}
);
故人爱我别走 2024-09-04 05:44:29

Date 的默认排序会将较新的日期放在较旧的日期之后,因此最旧的日期将位于列表的开头,而最新的日期将位于列表的末尾。在我看来,比较器一直很难阅读,所以我转而使用谷歌的Ordering 实现 Comparator 的对象更加简洁。例如,您的 Comparator 可以这样写:

Ordering<SomeObject> order = Ordering.natural().onResultOf(new Function<SomeObject, Date>() {
    public Date apply(SomeObject object) {
        return object.getDate();
    }
});
Comparator<SomeObject> comparator = order; // Ordering implements Comparable so this would be legal to do
Collections.sort(someList, order);

此代码创建的顺序 Comparator 将使用 Date 根据 SomeObject 对象的 Date 对它们进行排序。的自然排序。但是,Ordering 真正的好处是,一些额外的方法可以更改顺序,而无需编写更多逻辑,例如,要将日期顺序反转为最新到最旧,您只需添加一个调用撤销():

Ordering<SomeObject> order = Ordering.natural().reverse().onResultOf(new Function<SomeObject, Date>() {
    public Date apply(SomeObject object) {
        return object.getDate();
    }
});

The default ordering of Date will put newer dates after older dates so the oldest dates would be at the beginning of your list and the newest dates at the end. Comparators have always been hard to read in my opinion so I have switched to using google's Ordering objects that implement Comparator a little cleaner. For example your Comparator could be written like this:

Ordering<SomeObject> order = Ordering.natural().onResultOf(new Function<SomeObject, Date>() {
    public Date apply(SomeObject object) {
        return object.getDate();
    }
});
Comparator<SomeObject> comparator = order; // Ordering implements Comparable so this would be legal to do
Collections.sort(someList, order);

The order Comparator that this code created would sort SomeObject objects based on their Date using the Date's natural ordering. But what makes Ordering really nice is some of extra methods change the order without having to write any more logic, for example to reverse the order of dates to be newest to oldest you just have to add a call to reverse():

Ordering<SomeObject> order = Ordering.natural().reverse().onResultOf(new Function<SomeObject, Date>() {
    public Date apply(SomeObject object) {
        return object.getDate();
    }
});
若水微香 2024-09-04 05:44:29

这是旧的,但可能有人可以使用它。可以使用java8进行排序,如下所示:

  someList.sort(Comparator.comparing(listMember::dateProducingMethod))

This is old but may be someone can use it. It may be sorted using java8 as follows:

  someList.sort(Comparator.comparing(listMember::dateProducingMethod))
听风吹 2024-09-04 05:44:29

通过使用 lambdaj 您可以通过更简单、更易读的方式获得相同的结果,如下所示:

sort(someList, on(SomeObject.class).getSomeDate());

比编写一个不起眼的内部类要好得多,不是吗?

By using lambdaj you could achieve the same result in an easier and more readable way as it follows:

sort(someList, on(SomeObject.class).getSomeDate());

Far better than writing an obscure inner class, isn't it?

横笛休吹塞上声 2024-09-04 05:44:29

试试这个:

  List<Date> list=new ArrayList<Date>();
  //add some dates to list
  Collections.sort(list, new Comparator<Date>() {
      public int compare(final Date object1, final Date object2) {
          return Long.compare(object1.getTime(),object2.getTime());
      }}
  );

Date.getTime() 将日期“转换”为 long,这样更容易比较和排序。

无论如何,在幕后 Long 与此进行比较:

public static int compare(long x, long y) {
    return (x < y) ? -1 : ((x == y) ? 0 : 1);
}

如果你想反转排序,只需乘以 -1 即可,如下所示:

  List<Date> list=new ArrayList<Date>();
  //add some dates to list
  Collections.sort(list, new Comparator<Date>() {
      public int compare(final Date object1, final Date object2) {
          return Long.compare(object1.getTime(),object2.getTime())*-1;
      }}
  );

Try this:

  List<Date> list=new ArrayList<Date>();
  //add some dates to list
  Collections.sort(list, new Comparator<Date>() {
      public int compare(final Date object1, final Date object2) {
          return Long.compare(object1.getTime(),object2.getTime());
      }}
  );

Date.getTime() "converts" the date to a long, which is easier to compare and sort.

Anyway behind the curtain Longs are compared with this:

public static int compare(long x, long y) {
    return (x < y) ? -1 : ((x == y) ? 0 : 1);
}

If you want to invert the sort, just multiply by -1 like this:

  List<Date> list=new ArrayList<Date>();
  //add some dates to list
  Collections.sort(list, new Comparator<Date>() {
      public int compare(final Date object1, final Date object2) {
          return Long.compare(object1.getTime(),object2.getTime())*-1;
      }}
  );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文