Java arrays array sorting sample share

  • 2020-04-01 03:03:54
  • OfStack

The Java API's description of the Arrays class is that it contains the various methods used to manipulate Arrays, such as sorting and searching.

1. Sort arrays of basic data types

Description:

(1) sort() in the Arrays class USES the "tuned quicksort method";

(2) for example, int[], double[], char[] and other array types of radix data, the Arrays class only provides the default ascending order, and does not provide the corresponding descending order method.

(3) to sort the array of the underlying type in descending order, it is necessary to convert these arrays into the corresponding array of wrapper classes, such as Integer[], Double[], Character[], etc., to sort the array of these classes. In fact, it is better to do the ascending sort first, in their own order).

Sort the array in the default ascending order

  Function prototype: static void sort(int[] a)    Sorts the specified int array in ascending numeric order.

Int void sort(int[] a, int fromIndex, int toIndex)  Sorts the specified range of the specified int array in ascending numeric order.

Code example:


import java.util.Arrays;
public class ArraysSort_11 {
    public static void main(String args[])
    {
        int[] a={1,4,-1,5,0};
        Arrays.sort(a);
        //The contents of array a[] become {-1,0,1,4,5}
        for(int i=0;i<a.length;i++)
            System.out.print(a[i]+"  ");
    }
}

2. Sort the data of the composite data type

Function prototype:

(1) public static< T> Void sort(T[] a, Comparator c)& PI; Sorts the array of specified objects in the order produced by the specified comparator.

(2) public static< T> Void sort(T[] a, int fromIndex, int toIndex, Comparator c)& PI; Sorts the specified range of the specified array of objects in the order produced by the specified comparator.

Description: the two sorting algorithm is the "adjusted merge sort" algorithm.

Code example:


package aa;
import java.util.Arrays;
import java.util.Comparator;
public class Arraysort {
    Point[] arr;

    Arraysort(){
        arr=new Point[4];    //Defines an array of objects, arr, and allocates storage space
        for(int i=0;i<4;i++)
            arr[i]=new Point();
    }

    public static void main(String[] args) {

        Arraysort sort=new Arraysort();
        sort.arr[0].x=2;sort.arr[0].y=1;    //Initialize the data in the object array
        sort.arr[1].x=2;sort.arr[1].y=2;
        sort.arr[2].x=1;sort.arr[2].y=2;
        sort.arr[3].x=0;sort.arr[3].y=1;

        Arrays.sort(sort.arr, new MyComprator());    //Sort, using the specified collator
        for(int i=0;i<4;i++)    //Output sort result
            System.out.println("("+sort.arr[i].x+","+sort.arr[i].y+")");
    }
}
class Point{
    int x;
    int y;
}
//Comparator, x coordinates sorted from small to large; So if we have the same x, we're going to go from small to large
class MyComprator implements Comparator {
    public int compare(Object arg0, Object arg1) {
        Point t1=(Point)arg0;
        Point t2=(Point)arg1;
        if(t1.x != t2.x)
            return t1.x>t2.x? 1:-1;
        else
            return t1.y>t2.y? 1:-1;
    }
}


< img border = 0 id = theimg onclick = window. The open this. (SRC) SRC = "/ / files.jb51.net/file_images/article/201402/20140227145033.jpg? 2014127145059 ">


Related articles: