java - How to sort List of Map String in ascending order -
i have list of hashmap , want sort in ascending order in java.
i using custom comparator implement logic, failing. grouping list based on map keys. not sorting list entirely.
my code looks like-
public class listmap {      public static void main(string[] args) {         calendar cal = calendar.getinstance();         list<hashmap<string, string>> list = new arraylist<>();         (int = 0; < 20; i++) {             int value = randint(1, 20);             long timeinmillis = cal.gettimeinmillis();             hashmap<string, string> mp = new hashmap<>();             mp.put("" + value, "values" + value + ":" + timeinmillis);             list.add(mp);         }          collections.sort(list, new comparator<map<string, string>>() {             @override             public int compare(map<string, string> map1,                     map<string, string> map2) {                 string val1 = "";                 string val2 = "";                 (map.entry<string, string> entry : map1.entryset()) {                     val1 = entry.getkey();                 }                 (map.entry<string, string> entry : map2.entryset()) {                     val2 = entry.getkey();                 }                 return val1.compareto(val2);             }         });          (map<string, string> hashmap : list) {             (map.entry<string, string> entry : hashmap.entryset()) {                 system.out.println(entry.getkey() + "/" + entry.getvalue());             }         }      }      public static int randint(int min, int max) {         random rand = new random();         int randomnum = rand.nextint((max - min) + 1) + min;          return randomnum;     }  }   it prints output-
10/values10:1437660285301 11/values11:1437660285301 11/values11:1437660285301 11/values11:1437660285301 13/values13:1437660285301 13/values13:1437660285301 15/values15:1437660285301 15/values15:1437660285301 15/values15:1437660285301 16/values16:1437660285301 17/values17:1437660285301 18/values18:1437660285301 18/values18:1437660285301 19/values19:1437660285301 19/values19:1437660285301 2/values2:1437660285301 3/values3:1437660285301 4/values4:1437660285301 6/values6:1437660285301 6/values6:1437660285301   it groups values key, want entire sorted list in above example.
i expecting 2 , 3, 4 , go on.
you're comparing based on keys of type string. comparison done lexicographically, i.e. 12 less 2.
you need compare keys integers in comparator:
collections.sort(list, new comparator<map<string, string>>() {     @override     public int compare(map<string, string> map1,              map<string, string> map2) {         string val1 = "";         string val2 = "";         (map.entry<string, string> entry : map1.entryset()) {             val1 = entry.getkey();         }         (map.entry<string, string> entry : map2.entryset()) {             val2 = entry.getkey();         }          return integer.valueof(val1).compareto(integer.valueof(val2));     } });      
Comments
Post a Comment