另外一个排序已排序列表 [英] Further Sort an already sorted list

查看:163
本文介绍了另外一个排序已排序列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

地图列表,我已经由一个类型排序,但现在我想进一步梳理该名单

I have a list of maps that I have already sorted by one type but now I want to further sort that list

原始排序

Collections.sort(list, new Comparator<Map<String,Object>>(){

  @Override
  public int compare(Map<String, Object> m1,Map<String, Object> m2) {

      return m1.get("type").toString().compareTo(m2.get("type").toString()); //type is a 1 or 0
  }         
});

现在我想要做的就是保持这种秩序,但我想由人姓氏排序进一步的列表。我该怎么让这个刚刚拆分成2个列表,类型的一个列表 0 和另一个类型 1 然后添加的列表一起?

Now what I want to do is keep that order but I want to sort that list further by the persons last name. Am I going to have to split this into 2 lists, one list of type 0 and another with type 1 then just add the lists together?

基本上我想复制一个SQL排序,我会做键入ASC,姓氏ASC ,并给我按类型以按姓氏排序类型排序的列表。

basically I want to replicate a SQL sort where I would do type ASC, lastName ASC and give me a list sorted by type with the types sorted by last name.

推荐答案

您可以这样做:

int compareType = m1.get("type").toString().compareTo(m2.get("type").toString());
if(compareType != 0) {
   return compareType;
}
return m1.get("name").toString().compareTo(m2.get("name").toString());

如果您是在很多关键的排序,这将是更清洁的:

If you are sorting on many keys this would be cleaner:

for(String key : new String[] { "type", "name" }) {
   int compare = m1.get(key).toString().compareTo(m2.get(key).toString());
   if(compare != 0) {
      return compare;
   }
}
return 0;

这篇关于另外一个排序已排序列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆