- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含 LocalDateTime
字段的对象列表。
我想根据日期和时间对所有这些对象进行非常特别的排序。
我解释一下应该怎么做:
按日期降序排序BUT按时间升序
这是一个例子:
未排序 LocalDateTime
:
应该按这个顺序排序:
请记住,我需要对带有字段的对象进行排序,而不是 LocalDateTime
的列表,而是带有 LocalDateTime
字段的对象列表。
谢谢你帮助我:)
最佳答案
LocalDateTime
对象知道如何按时间顺序对自己进行排序。您希望按日期降序排序(倒序,较晚的日期在前),但也按时间升序排序(按时间顺序),这意味着该类实现的内置功能 compareTo
方法(Comparable
接口(interface)需要)不能完成这项工作。
对于自定义排序,请编写您自己的 Comparator
执行。该接口(interface)需要实现一种方法:compare
.
这里的逻辑很简单:
代码。
package work.basil.example;
import java.time.LocalDateTime;
import java.util.Comparator;
public class LocalDateTimeComparator implements Comparator < LocalDateTime >
{
@Override
public int compare ( LocalDateTime o1 , LocalDateTime o2 )
{
// Compare the date portion first. If equal, then look at time-of-day.
int result = o1.toLocalDate().compareTo( o2.toLocalDate() ); // Consider only the date portion first.
result = ( ( - 1 ) * result ); // Flip the positive/negative sign of the int, to get ascending order. Or more simply: `= - result ;`.
if ( 0 == result ) // If dates are equal, look at the time-of-day.
{
System.out.println( "reversing " );
result = o1.toLocalTime().compareTo( o2.toLocalTime() );
}
return result;
}
}
用这个比较器试试你的示例数据。
List < LocalDateTime > ldts = List.of(
LocalDateTime.parse( "2016-10-06T09:10" ) ,
LocalDateTime.parse( "2016-10-06T10:34" ) ,
LocalDateTime.parse( "2016-12-06T11:15" ) ,
LocalDateTime.parse( "2016-11-06T10:34" ) ,
LocalDateTime.parse( "2016-12-06T10:10" ) ,
LocalDateTime.parse( "2016-12-06T06:56" )
);
List < LocalDateTime > sorted = new ArrayList <>( ldts );
Comparator < LocalDateTime > comparator = new LocalDateTimeComparator();
sorted.sort( comparator );
转储到控制台。我们看到了成功。日期按 2016 年 10 月、11 月和 12 月的日期顺序排列,而时间则按日期排列
System.out.println( "ldts = " + ldts );
System.out.println( "sorted = " + sorted );
ldts = [2016-10-06T09:10, 2016-10-06T10:34, 2016-12-06T11:15, 2016-11-06T10:34, 2016-12-06T10:10, 2016-12-06T06:56]
sorted = [2016-12-06T06:56, 2016-12-06T10:10, 2016-12-06T11:15, 2016-11-06T10:34, 2016-10-06T09:10, 2016-10-06T10:34]
comment by Ole V.V.展示了如何在现代 Java 中使用函数式 lambda 语法进行等效工作。该评论启发我尝试函数式方法。
这里的想法是使用两个 Comparator
对象:一个用于日期,一个用于时间。我们可以通过调用 Comparator::thenComparing
将一个 Comparator
嵌套在另一个中。所以我们需要建立两个比较器,然后将一个提供给另一个。我们实例化一个comparatorDate
,然后给它一个comparatorTime
,得到一个comparatorDateThenTime
。我们将 comparatorDateThenTime
传递给 sort
方法以实际执行排序工作。
List < LocalDateTime > ldts = List.of(
LocalDateTime.parse( "2016-10-06T09:10" ) ,
LocalDateTime.parse( "2016-10-06T10:34" ) ,
LocalDateTime.parse( "2016-12-06T11:15" ) ,
LocalDateTime.parse( "2016-11-06T10:34" ) ,
LocalDateTime.parse( "2016-12-06T10:10" ) ,
LocalDateTime.parse( "2016-12-06T06:56" )
);
List < LocalDateTime > sorted = new ArrayList <>( ldts );
Comparator < LocalDateTime > comparatorDate =
Comparator
.comparing( ( LocalDateTime ldt ) -> ldt.toLocalDate() )
.reversed();
Comparator < LocalDateTime > comparatorTime =
Comparator
.comparing( ( LocalDateTime ldt ) -> ldt.toLocalTime() );
Comparator < LocalDateTime > comparatorDateThenTime =
comparatorDate
.thenComparing(
comparatorTime
);
sorted.sort( comparatorDateThenTime );
// Dump to console.
System.out.println( "ldts = " + ldts );
System.out.println( "sorted = " + sorted );
ldts = [2016-10-06T09:10, 2016-10-06T10:34, 2016-12-06T11:15, 2016-11-06T10:34, 2016-12-06T10:10, 2016-12-06T06:56]
sorted = [2016-12-06T06:56, 2016-12-06T10:10, 2016-12-06T11:15, 2016-11-06T10:34, 2016-10-06T09:10, 2016-10-06T10:34]
我们可以使用从调用 Comparator.comparing
和 Comparator.reversed
中返回的匿名 Comparator
对象,使用单线器将所有这些组合在一起.
List < LocalDateTime > ldts = List.of(
LocalDateTime.parse( "2016-10-06T09:10" ) ,
LocalDateTime.parse( "2016-10-06T10:34" ) ,
LocalDateTime.parse( "2016-12-06T11:15" ) ,
LocalDateTime.parse( "2016-11-06T10:34" ) ,
LocalDateTime.parse( "2016-12-06T10:10" ) ,
LocalDateTime.parse( "2016-12-06T06:56" )
);
List < LocalDateTime > sorted = new ArrayList <>( ldts );
sorted.sort(
Comparator
.comparing( ( LocalDateTime ldt ) -> ldt.toLocalDate() )
.reversed()
.thenComparing(
Comparator
.comparing( ( LocalDateTime ldt ) -> ldt.toLocalTime() )
)
);
// Dump to console.
System.out.println( "ldts = " + ldts );
System.out.println( "sorted = " + sorted );
ldts = [2016-10-06T09:10, 2016-10-06T10:34, 2016-12-06T11:15, 2016-11-06T10:34, 2016-12-06T10:10, 2016-12-06T06:56]
sorted = [2016-12-06T06:56, 2016-12-06T10:10, 2016-12-06T11:15, 2016-11-06T10:34, 2016-10-06T09:10, 2016-10-06T10:34]
我想我更愿意在生产代码中看到第一个,即多行的。但我不确定。
问题中陈述的真正问题涉及 LocalDateTime
作为另一个类的成员字段。因此,让我们扩展我们的解决方案以包含该嵌套类。在这里,我们发明了一个 Happening
类,该类由一个描述字符串和一个 LocalDateTime
对象组成。
package work.basil.example;
import java.time.LocalDateTime;
import java.util.Objects;
public class Happening
{
private String description;
private LocalDateTime localDateTime;
public Happening ( String description , LocalDateTime localDateTime )
{
this.description = Objects.requireNonNull( description );
this.localDateTime = Objects.requireNonNull( localDateTime );
}
public String getDescription ( ) { return this.description; }
public LocalDateTime getLocalDateTime ( ) { return this.localDateTime; }
@Override
public String toString ( )
{
return "Happening{ " +
"description='" + description + '\'' +
" | localDateTime=" + localDateTime +
" }";
}
}
让我们收集这些对象,并使用类似于上面看到的代码进行排序。我们必须多做一步,从每个 Happening
对象中提取一个 LocalDateTime
对象。
List < Happening > happenings = List.of(
new Happening( "aaa" , LocalDateTime.parse( "2016-10-06T09:10" ) ) ,
new Happening( "bbb" , LocalDateTime.parse( "2016-10-06T10:34" ) ) ,
new Happening( "ccc" , LocalDateTime.parse( "2016-12-06T11:15" ) ) ,
new Happening( "ddd" , LocalDateTime.parse( "2016-11-06T10:34" ) ) ,
new Happening( "eee" , LocalDateTime.parse( "2016-12-06T10:10" ) ) ,
new Happening( "fff" , LocalDateTime.parse( "2016-12-06T06:56" ) )
);
List < Happening > sorted = new ArrayList <>( happenings );
sorted.sort(
Comparator
.comparing( ( Happening happening ) -> happening.getLocalDateTime().toLocalDate() )
.reversed()
.thenComparing(
Comparator
.comparing( ( Happening happening ) -> happening.getLocalDateTime().toLocalTime() )
)
);
// Dump to console.
System.out.println( "happenings = " + happenings );
System.out.println( "sorted = " + sorted );
运行时,我们从 a-b-c-d-e-f 到 f-e-c-d-a-b 顺序。
happenings = [Happening{ description='aaa' | localDateTime=2016-10-06T09:10 }, Happening{ description='bbb' | localDateTime=2016-10-06T10:34 }, Happening{ description='ccc' | localDateTime=2016-12-06T11:15 }, Happening{ description='ddd' | localDateTime=2016-11-06T10:34 }, Happening{ description='eee' | localDateTime=2016-12-06T10:10 }, Happening{ description='fff' | localDateTime=2016-12-06T06:56 }]
sorted = [Happening{ description='fff' | localDateTime=2016-12-06T06:56 }, Happening{ description='eee' | localDateTime=2016-12-06T10:10 }, Happening{ description='ccc' | localDateTime=2016-12-06T11:15 }, Happening{ description='ddd' | localDateTime=2016-11-06T10:34 }, Happening{ description='aaa' | localDateTime=2016-10-06T09:10 }, Happening{ description='bbb' | localDateTime=2016-10-06T10:34 }]
关于java - 按 LocalDate 降序和 LocalTime 升序对列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62392699/
我是一名优秀的程序员,十分优秀!