【问题标题】:How to sort ArrayList with two properties? [duplicate]如何对具有两个属性的 ArrayList 进行排序? [复制]
【发布时间】:2018-07-05 00:02:43
【问题描述】:

我有一个 Arraylist,我想按两个属性对 ArrayList 进行排序 1.按姓名(按字母顺序) 2.通过自定义键

更多解释-:

ArrayList<User> list = new ArrayList();

Collections.sort(list, new Comparator<User>() {
        @Override
        public int compare(User user, User t1) {
        1.By User.getName() (Alphabetically)
        2.By User.getRelationshipState() (Custom property)


});

`

【问题讨论】:

  • By a custom Key 是什么?
  • “自定义密钥”是什么意思?您想在每次排序时更改要排序的键吗?或者这只是User 类的某个字段?
  • 自定义键如 user.getRelationshipState 是 public final static int REQNOTSENT =0;公共最终静态 int REQUESTSENT =1;公共最终静态int FRIENDS = 2;公共最终静态 int REQUESTRCVED =3;我想像 Friends REQUESTSENT REQUESTRCVED REQNOTSENT 一样排序

标签: java android sorting arraylist


【解决方案1】:

您的对象需要实现 Comparable 接口。

See javadoc

【讨论】:

  • 请您解释一下这个垃圾位吗?
  • 这听起来不像是潜在的评论。
  • 抱歉,但这不会比他已经尝试过的 Comparator 更进一步。
猜你喜欢
  • 2012-08-05
  • 1970-01-01
  • 2013-06-14
  • 2012-10-01
  • 1970-01-01
  • 2019-04-06
  • 2020-12-20
  • 2022-11-23
  • 2013-02-08
相关资源
最近更新 更多