java 根据键值对HashMap的ArrayList进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13549065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sorting ArrayList of HashMap based on key value
提问by intrepidkarthi
I have a list of HashMaps. Each HashMap consists of several kay-value pairs and everything comes as a string. I am storing all the hashmaps inside an arraylist. Now I need to sort the arraylist based on the key inside the hashmap.
我有一个 HashMap 列表。每个 HashMap 由几个 kay-value 对组成,一切都以字符串的形式出现。我将所有哈希图存储在一个数组列表中。现在我需要根据哈希图中的键对数组列表进行排序。
Here is my sample data:
这是我的示例数据:
{
"productID":"5643",
"productName":"Apple - iPod touch",
"outsidePrice":"189.99",
"merchantID":"134439",
"ourPrice":"184.99",
"storeName":"Ebay",
}
{
"productID":"3243",
"productName":"Apple - iPad",
"outsidePrice":"389.99",
"merchantID":"54439",
"ourPrice":"384.99",
"storeName":"Apple",
}
I am storing this data inside this structure.
我将这些数据存储在这个结构中。
ArrayList<HashMap<String, String>> data_list = new ArrayList<HashMap<String, String>>();
I have a huge list of items like this. Now I need to sort the arraylist based on the productName, Price, storeName, productID fields inside the hashmap.
我有很多这样的项目。现在我需要根据哈希图中的 productName、Price、storeName、productID 字段对数组列表进行排序。
回答by Simon
I recommend that you use a custom product class to do this for you. It will ultimately make your code easier to maintain and more robust, IMHO.
我建议您使用自定义产品类来为您执行此操作。恕我直言,它最终将使您的代码更易于维护和更健壮。
How about this?
这个怎么样?
A class to represent your data:
表示数据的类:
class Product{
public string productId;
public string productName;
public BigDecimal outsidePrice;
public int merchantId;
public BigDecimal ourPrice;
public string storeName;
// whatever constuctors you need
}
A List of your products:
您的产品清单:
List<Product> products;
Now define a Comparatorto sort, one for each field that you need to sort on. Here is an example for productId.
现在定义一个Comparator进行排序,每个需要排序的字段对应一个。这是 productId 的示例。
public class ProductProductIdComparator implements Comparator<Product>{
@Override
public int compare(Product product1, Product product2) {
if (product1.productId > product2.productId){
return +1;
}else if (product1.productId < product2.productId){
return -1;
}else{
return 0;
}
}
}
And finally, a Collections sortwhich accepts a comparator as an argument:
最后,一个集合排序,它接受一个比较器作为参数:
Collections.sort(products, new ProductProductIdComparator());
回答by Perception
The Collectionsclass provides a utility method for sorting a list in place, using a Comparator.
的类别类提供了在适当位置排序的列表,使用比较器的实用程序的方法。
final List<Map<String, String>> dataList = new ArrayList<HashMap<String, String>>(4);
Collections.sort(dataList, new Comparator<Map<String, String>>() {
@Override
public int compare(final Map<String, String> map1, final Map<String, String> map2) {
// Get fields from maps, compare
}
}
回答by Sajid
You can use arrays.sort() to achieve this in short, at some minor memory cost.
简而言之,您可以使用 arrays.sort() 来实现这一点,但需要一些较小的内存成本。
HashMap[] result = Arrays.sort(list.toArray(), new Comparator() {
public void compare(Object o1, Object o2) {
HashMap<String, String> a = (HashMap<String, String>)o1;
HashMap<String, String> b = (HashMap<String, String>)o2;
// return value as per contract of Comparator.compare() doing whatever comparisons you need.
}
public boolean equals(Object obj) { return this == obj; }
});