我想按一个参数排序我的对象列表,它的日期格式为“YYYY-MM-DD HH:mm”按升序排序。我找不到合适的解决方案。在python中很容易使用lambda对它进行排序,但在Java中我遇到了问题。
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());
}
});
提前致谢!
答案 0 :(得分:1)
你能试试吗?我认为它会起作用:
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();
更新: 如果你想要一个清单:
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());
更新:(使用“汽车”更新问题)
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());
答案 1 :(得分:0)
如果您使用的是java 8:
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:
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...
}
}
});
答案 2 :(得分:0)
没有foreach($records as $week=>$record)
{
echo $week; // gives week 1, week 2 , week 3..
foreach($record as $r)
{
echo $r;
}
}
的清洁解决方案:
ParseException
答案 3 :(得分:0)
公共布尔值(日期时间)
当且仅当此日期所代表的时间瞬间时才为真 对象严格早于当时表示的瞬间;假 否则。
请参阅java文档了解更多信息https://docs.oracle.com/javase/8/docs/api/java/util/Date.html#before-java.util.Date-
或者你可以在日期类之前使用之后的取代
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);
}
}
答案 4 :(得分:-1)
我的解决方案的修订版。如果您定义以下比较器类...
class ShopDateComparator implements Comparator<Shop> {
@Override
public int compare(Shop shop1, Shop shop2) {
return shop1.getDate().toLowerCase().compareTo(shop2.getDate().toLowerCase());
}
}
...然后您需要做的所有排序cars
(我假设是Shop
类型的对象列表)是:
Collections.sort(cars, new ShopDateComparator());