【问题标题】:How to Sort Date in ArrayList in Java [duplicate]如何在Java中对ArrayList中的日期进行排序[重复]
【发布时间】:2017-07-28 06:44:32
【问题描述】:

我有一个 Map> 这里的键是 Employee 的 EmpId,键将包含 List

BonusEntity 将包含以下字段(所有字段均为字符串)

Empid 奖金日期 奖金金额 部门

我需要根据 BonusDate 降序对 Map 中的 ArrayList 进行排序,谁能帮忙提前谢谢。

【问题讨论】:

  • 请提高您的提问质量。您可以在以下位置找到更多提示:How do I ask a good question?How to create a Minimal, Complete, and Verifiable example 页面。
  • 实体类可以实现 Comparable 实现 Compare to as @Override public int compareTo(BonusEntity o) { return o.bonusdate.compareTo(this.bonusdate);并使用 Collections.sort(list) 对列表进行排序;请记住,使用字符串来保存日期值会给您带来一些意想不到的结果,请使用 Date 类而不是字符串

标签: java sorting arraylist collections


【解决方案1】:

您应该创建一个适用于您的班级BonusEntityComparator,并通过Collections.sort method 使用它。

【讨论】:

  • 或者,让BonusEntity 实现Comparable<BonusEntity> 然后调用List.sort()
  • 也可以,使用Comparable,您将为该类定义一种自然排序。您可以根据需要/想象定义任意数量的Comparator
猜你喜欢
  • 2013-08-28
  • 1970-01-01
  • 2013-07-05
  • 1970-01-01
  • 2019-10-05
  • 2017-02-09
  • 1970-01-01
  • 2016-05-28
  • 1970-01-01
相关资源
最近更新 更多