Nested list example:
[[' AA ', ' BB ', ' 2 "," NN "],[" AA "," BB "," a "," NN "],[" AA "," BB "," a "," NN "],[" AA "," BB "," a "," NN "],[" AA ", "BB", "],[", "nn", "AA", "BB", "I", "nn"]
we want to sort the third field of the list, which is index=2. The result of the
after sorting is still nested list, but the order of the output is changed. The
has tried several ways, eventually locking the TreeMap to complete the sort operation. The
code is as follows:
Package javaheap;
Import java.util.ArrayList;
Import Java.util.Comparator;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Set;
Import Java.util.TreeMap;
public class Sort {public static void main (string[] args) {list<string> ll = new arraylist<> ();
list<list<string>> List = new arraylist<list<string>> ();
Ll.add ("AA");
Ll.add ("BB");
Ll.add ("2");
Ll.add ("nn");
List.add (LL);
ll = new arraylist<> ();
Ll.add ("AA");
Ll.add ("BB");
Ll.add ("32");
Ll.add ("nn");
List.add (LL);
ll = new arraylist<> ();
Ll.add ("AA");
Ll.add ("BB");
Ll.add ("22");
Ll.add ("nn");
List.add (LL);
SYSTEM.ERR.PRINTLN (list);
List<list<string>> li = Orderbyevgdesc (List);
System.err.println (LI); } private static List<list<string>> Orderbyevgdesc (list<list<string>> list1) {list<list<string>> List = new Arraylist<lis
T<string>> (); Map<integer, list<string>> map = new Treemap<integer, list<string>> (New Compa
Rator<integer> () {public int compare (integer obj1, integer obj2) {
Descending sort return Obj2.compareto (OBJ1);
}
});
for (int i = 0; i < list1.size (); i++) {Map.put (Integer.parseint (List1.get (i). Get (2)), List1.get (i));
} set<integer> keyset = Map.keyset ();
Iterator<integer> iter = Keyset.iterator ();
while (Iter.hasnext ()) {Integer key = Iter.next ();
List.add (Map.get (key));
} return list;
}
}
Run Result:
Sort before: [[AA, BB, 2, nn], [AA, BB, nn], [AA, BB, nn]]
sorted: [[AA, BB, MB, nn], [AA, BB, nn], [AA, BB, 2, nn]]
Add:
TreeMap: A NAVIGABLEMAP implementation based on the red-black Tree (red-black), sorted according to the natural order of its keys, or sorted according to the Comparator provided when the mapping was created, depending on the construction method used.