sorting - Sort of Nested Maps, JAVA, innerMap not change keys from OuterMap -


i trying sort nestedmaps

map<integer,map<string,integer>>

integer,string,integer: firstkey id, innerkey name, innervalue phone. doing this:

           set<entry<integer,map<string,integer>>> set = add.entryset();            list<entry<integer,map<string,integer>>> list = new arraylist<>(set);            set<entry<string,integer>> set2 = add2.entryset();            list<entry<string,integer>> list2 = new arraylist<>(set2);            collections.reverse(list);            collections.reverse(list2); 

and looping see results, if have first 3 inputs id, name , phone: , have result:

id 3 - phone 2, name i2; id 2 - phone 1, name i; id 1 - phone 5, name p;

next sorting should have name, , depending on id-should change, doesnt work.

and how have sort name result this:

id 2 - phone 1, name i; id 3 - phone 2, name i2; id 1 - phone 5, name p;

first of all, if don't use wrapper objects cases yours require complex , chained data structures, code messy. , should give meaningful names wrapper data structures. make code easier read , understand.

for representing map<integer,map<string,integer>>, can use 2 wrapper classes, inner map map<wrapper1> wrapper1 denotes string, integer combinations. , can define outer map map<wrapper2> wrapper2 denotes integer , map<wrapper1> pairs.

and sorting out data, use treemap. long supply proper comparison logic wrapper instances, map automatically sorted treemap.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -