Java sort list object by date ascending
本问题已经有最佳答案,请猛点这里访问。
我想按一个参数对我的对象列表排序,它的日期格式为"YYYY-MM-DD HH:mm",按升序排列。我找不到合适的解决方案。在Python中,很容易使用lambda来排序它,但是在Java中,我遇到了一个问题。
1 2 3 4 5 6 7 | for (Shop car : cars) { Collections.sort(cars, new Comparator<Shop>() { @Override public int compare(final Shop car, final Shop car) { return car.getDate().compareTo(arc.getDate()); } }); |
事先谢谢!
你能试试吗?我认为它会起作用:
1 2 3 4 5 6 7 8 9 10 | SimpleDateFormat f = new SimpleDateFormat("YYYY-MM-DD HH:mm"); Stream<Date> sorted = l.stream().map(a->{ try { return f.parse(a); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }).sorted(); |
更新:如果你想要一个列表:
1 2 3 4 5 6 7 8 9 | List sorted = l.stream().map(a->{ try { return f.parse(a); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }).sorted().collect(Collectors.toList()); |
更新:(问题使用"汽车"更新)
1 2 3 4 5 6 7 8 9 10 11 12 13 | SimpleDateFormat f = new SimpleDateFormat("YYYY-MM-DD HH:mm"); List<Car> sorted = cars.stream().sorted( (a,b)-> { try { return f.parse(a.getDate()).compareTo(f.parse(b.getDate())); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return 0; } ).collect(Collectors.toList()); |
public boolean before(Date when)
true if and only if the instant of time represented by this Date
object is strictly earlier than the instant represented by when; false
otherwise.
请参考Java文档以获取更多信息http://DOCS.Oracle .COM/JavaSe/ 8 /DOCS/API/Java/UTL/DATE。
或者,您可以使用after方法从日期类代替before
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | package com.stackoverflow.DataSortReverse; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.List; class Car{ private String name; private Date date; public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } public Car(String name, Date date) { super(); this.name = name; this.date = date; } @Override public String toString() { return"Car [date=" + date +"]"; } } public class DateSort { public static void main(String[] args) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); List<Car> carList = new ArrayList<Car>(); try { carList.add(new Car("car1",dateFormat.parse("2017-01-10"))); carList.add(new Car("car1",dateFormat.parse("2017-02-10"))); carList.add(new Car("car1",dateFormat.parse("2017-02-30"))); carList.add(new Car("car1",dateFormat.parse("2017-01-09"))); } catch (ParseException e) { System.out.println(e.getMessage()); } /* * if you wish to change sorting order just * replace -1 with 1 and 1 with -1 * * * date1.before(date2) returns true when date1 comes before date2 * in calendar * * java docs :: https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#before-java.util.Date- * */ Collections.sort(carList, new Comparator<Car>() { @Override public int compare(Car o1, Car o2) { if(o1.getDate().before(o2.getDate())){ return -1; } return 1; } }); System.out.println(carList); } } |
不含
1 2 | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); list.sort(Comparator.comparing(shop -> LocalDateTime.parse(shop.getDate(), formatter))); |
如果您使用的是Java 8:
1 2 3 | DateTimeFormatter fm = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); objects.sort((o1, o2) -> LocalDateTime.parse(o1.getDateStr(), fm) .compareTo(LocalDateTime.parse(o2.getDateStr(), fm))); |
Java 7:
1 2 3 4 5 6 7 8 9 10 | DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm"); Collections.sort(objects, new Comparator<YourObjectType>() { public int compare(YourObjectType o1, YourObjectType o2) { try { return df.parse(o1.getDateStr()).compareTo(df.parse(o2.getDateStr())); } catch(ParseException pe) { // handle the way you want... } } }); |
我的解决方案的修订版。如果定义下面的Comparator类…
1 2 3 4 5 6 | class ShopDateComparator implements Comparator<Shop> { @Override public int compare(Shop shop1, Shop shop2) { return shop1.getDate().toLowerCase().compareTo(shop2.getDate().toLowerCase()); } } |
…那么您只需要对
1 |