0

i am using java and try to loop over map in ascending and descending order. i tried

NavigableMap<String, List<Person>> nm =  new TreeMap<String, List<person>>();
        nm.put("102", Listofperson1);
        nm.put("102_104", listofperson2);
        nm.put("103_102", listofperson3);
        nm.put("104_105", listofperson4);
        nm.put("106_107", listofperson5);
        nm.put("108", listofperson6);

Sting pointer key = 103_102; Person object has Name,Id,Age

i want to sort ascending order the map by age and iterate over the map as below: 1)if ponterkey != firstkey or last key then fisrt key to pointer key map should iterate descending order of age and pointer key to last key should iterate ascending order of age.

i have tried :

NavigableMap<String, List<Person>> nmAsc =  new TreeMap<String, List<person>>();
nmAsc.putAll(sortmethod(nm)) ;

NavigableMap<String, List<Person>> submapdesc =  new TreeMap<String, List<person>>();
submapdesc.putAll(sortmethod(nmAsc .subMap(firstkey,true,pointerkey,false))) ;

NavigableMap<String, List<Person>> submapasc =  new TreeMap<String, List<person>>();
submapdesc.putAll(sortmethod(nmAsc .subMap(pointerkey,false,lastkey,true))) ;

for (submapdesc ){}

for (submapasc ){}

sort method is correct.but when put all to the sub map it's order not working.your suggestions are highly appreciated.

edmaa
  • 121
  • 1
  • 1
  • 11
  • A TreeMap is ordered by key, you can't order the elements yourself (as in without using TreeMap's ordering, which you can specify yourself), it can't be ordered on value and the insertion order is irrelevant. You're probably looking for some sort of List instead. – Bernhard Barker Mar 08 '18 at 16:44
  • Possible duplicate of [Sort a Map by values](https://stackoverflow.com/questions/109383/sort-a-mapkey-value-by-values) – Bernhard Barker Mar 08 '18 at 16:51

0 Answers0