Package test; import java.util.ArrayList; import Java.util.collections;import Java.util.comparator;import Java.util.hashmap;import Java.util.list;import Java.util.Map; Public classJava_listmapsort { Public Static voidMain (string[] args) {//TODO auto-generated Method Stublist<map<string, object>> list =NewArraylist<map<string, object>>(); Map<string, object> map1 =NewHashmap<string, object>(); Map1.put ("name","P"); Map1.put (" Age","5"); Map<string, object> map2 =NewHashmap<string, object>(); Map2.put ("name","h"); Map2.put (" Age"," A"); Map<string, object> map3 =NewHashmap<string, object>(); Map3.put ("name","F"); Map3.put (" Age"," to"); List.add (MAP1); List.add (MAP3); List.add (MAP2); //before sorting for(Map<string, object>map:list) {System. out. println (Map.Get(" Age")); } collections.sort (List,NewComparator<map<string, object>>() { Public intCompare (map<string, object> O1, map<string, object>O2) {Integer Age1= Integer.valueof (O1.Get(" Age"). ToString ());//Age1 is the first age to be taken out of your list.Integer age2= integer.valueof (O2.Get(" Age"). ToString ());//Age1 is the second age to be taken out of your list. returnAge1.compareto (Age2); Return Age2.compareto (Age1), Descending}}); //after sortingSystem. out. println ("-------------------"); for(Map<string, object>map:list) {System. out. println (Map.Get(" Age")); } }}
The list map is sorted in Java based on the map key value