Java uses the comparator interface to implement custom sorting techniques.

Source: Internet
Author: User
    In practice, we often have to implement some of the more bizarre sort.

For example, in a group of [1,3,5,8,4,6,5], keep the original order unchanged, in the order of size. and establish a mapping relationship. (after sorting, use O (1) complexity to query the position of the first large number in the original array)
such as this
Index 0 1 2 3 4 5 6
Arr 1 3 5 8 4 6 5
Sort 6 5 2 0 4 1 3

    For the algorithm party, this time can hand tear a fast platoon or merge and so on. (But if it's in the game, it's not the time.)
       Wow, we have to make the most of the JDK.
Refers to this method of Arrays.sort (t[] A, comparator< Super t> C)).

Here is an example ... package sorts;
Import Java.util.Arrays;

Import Java.util.Comparator;
        public class Arraysorttest {static class array_test{int be_sort;
        Public array_test (int be_sort) {this.be_sort = Be_sort;

        } public static void Main (string[] args) {int[] arr_test = {10,2,6,9,60,47,8,5,6,1};
        array_test[] arr = new Array_test[arr_test.length]; 
        for (int i = 0; I<arr.length i++) {Arr[i] = new Array_test (arr_test[i)); } arrays.sort (arr, new comparator<array_test> () {public int compare (Array_test O1, array_test o
            2) {return o1.be_sort-o2.be_sort;
        }
        });
        System.out.print ("[");
        for (int i = 0; i<arr.length-1 i++) {System.out.print (arr[i].be_sort+ ",");
        } System.out.print (Arr[arr.length-1].be_sort); System.out.printlN ("]");
 }
}

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.