The "thought" nesting list is sorted for an index

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.