我將產品的詳細信息保存在地圖中,並添加到ArrayList中< HashMap < String,String >>並設置到自定義列表適配器。我需要按價格對價值進行排序。如何實現它?提前致謝。Android:如何訂購ArrayList <HashMap <String,String >>?
回答
普萊斯使用下面的代碼:
ArrayList< HashMap< String,String >> arrayList=populateArrayList();
Collections.sort(arrayList, new Comparator<HashMap< String,String >>() {
@Override
public int compare(HashMap<String, String> lhs,
HashMap<String, String> rhs) {
// Do your comparison logic here and retrn accordingly.
return 0;
}
});
+1和謝謝你,你的代碼幫助我(自第一次以來接受) –
,你可以簡單地使用這個在一個數組列表對象的自定義排序,
public class MyComparator implements Comparator<MyObject> {
@Override
public int compare(MyObject o1, MyObject o2) {
return o1.getYOUROBJ1STR.compareTo(o2.getYOUROBJ2STR);
}
}
讓我知道如果你仍然面臨的問題進行排序地圖
的+1你的代碼引導我 –
您可以實現Comparator<Map<String, String>>
或Comparator<HashMap<String, String>>
How sort an ArrayList of HashMaps holding several key-value pairs each? 回答得好:
class MapComparator implements Comparator<Map<String, String>>{
private final String key;
public MapComparator(String key){
this.key = key;
}
public int compare(Map<String, String> first,
Map<String, String> second){
// TODO: Null checking, both for maps and values
String firstValue = first.get(key);
String secondValue = second.get(key);
return firstValue.compareTo(secondValue);
}
}
...
Collections.sort(arrayListHashMap, new MapComparator("value"));
+1你的代碼引導我 –
不錯的代碼..它幫助的感謝.. +1也 –
我想你是s earching此: -
Sort ArrayList of custom Objects by property
sorting a List of Map<String, String>
希望它會幫助你...
+1您的代碼指導我 –
- 1. Android ArrayList <HashMap <String,String >>
- 2. 如何聲明HashMap <String,ArrayList <ArrayList <String> >>?
- 3. 構造函數ArrayAdapter <ArrayList <HashMap <String,String >>>(Context,int,ArrayList <HashMap <String,String >>)未定義
- 4. 將ArrayList <HashMap <String,String >>附加到另一個ArrayList <HashMap <String,String >>
- 5. 從ArrayList中獲取元素<HashMap <String,HashMap <String,String >>>
- 6. 如何正確聯合HashMap <String,ArrayList <Object>>和HashMap <String,Object>
- 7. Javapoet - TypeName - HashMap <String,HashMap <String,List <String> >> generation?
- 8. Map <String,Map <String,Boolean >> myMap = new HashMap <String,HashMap <String,Boolean >>();
- 9. HashMap <String,ArrayList <Objects>>
- 10. set ArrayList <HashMap <String,Object>> value
- 11. Android將ArrayList <HashMap <String,String >> mylist轉換爲stringarray
- 12. 如何從ArrayList中獲取對象<HashMap <String,String >>
- 13. 如何迭代Arraylist <HashMap <String,String >>?
- 14. 如何在SharedPreferences中存儲ArrayList <HashMap <String,String >>?
- 15. 如何在AlertDialog中設置ArrayList <HashMap <String,String >>?
- 16. 如何在SharedPreferences中存儲ArrayList <HashMap <String,String >>?
- 17. 加載數據到HashMap <String,HashMap <String,HashMap <String,ArrayList <ClassOb> >>>
- 18. 使用HashMap(String,ArrayList <String>)
- 19. 轉換ArrayList <ArrayList <String>>「返回」ArrayList <String>
- 20. java:在ArrayList中存儲HashMap <String,String><Object>
- 21. 從ArrayList中刪除重複項<HashMap <String,String >>
- 22. 在AsyncTask中傳遞ArrayList <HashMap <String,String >>
- 23. 獲取ArrayList <HashMap <String,String >>的值用於顯示
- 24. Group ArrayList <HashMap <String,String >>按某個鍵
- 25. 從ArrayList得到無效值<HashMap <String,String >>
- 26. 在ArrayAdapter中設置ArrayList <HashMap <String,String >>
- 27. ArrayList <HashMap <String,String >>沒有值
- 28. ArrayList <HashMap <String,String >>到JSON對象Java
- 29. 傳遞ArrayList <HashMap <String,String >>到一個片段
- 30. 從ArrayList填充ListView <HashMap <String,String >>
嘗試谷歌搜索在地圖排序。 – JMelnik
可能的重複[如何對HashMap的數組列表持有幾個鍵值對?](http://stackoverflow.com/questions/5369573/how-sort-an-arraylist-of-hashmaps-holding-several-key - 價值對 - 每個) –