分拣地图℃的列表,字符串,字符串> [英] sorting a List of Map<String, String>

查看:125
本文介绍了分拣地图℃的列表,字符串,字符串>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样创造了一个列表变量:

I have a list variable created like this:

名单,其中,地图<字符串,字符串>>名单=新的ArrayList<地图<字符串,字符串>>();

在我的Andr​​oid应用程序,这个名单被填充。

In my Android application, this list gets populated.

只是一个例子:

Map<String, String> map1 = new HashMap<String, String>();
map.put("name", "Josh");
...

Map<String, String> map2 = new HashMap<String, String>();
map.put("name", "Anna");
...

Map<String, String> map3 = new HashMap<String, String>();
map.put("name", "Bernie");
...

list.add(map1);
list.add(map2);
list.add(map3);

我使用列表显示结果在的ListView 延长 BaseAdapter 和实施的各种方法。

I am using list to show results in a ListView by extending BaseAdapter and implementing the various methods.

我的问题:我需要排序的字母顺序列表基于地图的关键名称

My problem: I need to sort list in alphabetical order based on the map's key name

问:什么是一个简单的方法来排序的字母顺序列表基于地图的关键名称

Question: What is a simple way to sort list in alphabetical order based on the map's key name?

我似乎无法环绕此我的头。我已经提取的每个名称每个地图字符串数组,排序它( Arrays.sort(strArray); )。但是,这并不preserve在其他各个数据地图,所以我也不太清楚,我怎么可以preserve其他映射值

I can't seem to wrap my head around this. I have extracted each name from each Map into a String array, and sorted it(Arrays.sort(strArray);). But that doesn't preserve the other data in each Map, so i'm not too sure how i can preserve the other mapped values

推荐答案

以下code完美的作品

The following code works perfectly

public Comparator<Map<String, String>> mapComparator = new Comparator<Map<String, String>>() {
    public int compare(Map<String, String> m1, Map<String, String> m2) {
        return m1.get("name").compareTo(m2.get("name"));
    }
}

Collections.sort(list, mapComparator);

不过,您的地图应可能是一个特定的类的实例。

But your maps should probably be instances of a specific class.

这篇关于分拣地图℃的列表,字符串,字符串&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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