A few days ago the Java test, encountered on the array of objects to sort, do not understand the principle of which, below to a more in-depth understanding. (The use of the specific sort () Reference API documentation)
the use of Arrays.sort () is mainly divided into the sort of array of basic data types and the sort of array of objects.
1. Sorting the array of basic data types
1> Numeric sorting:
int[] Intarray = new int[]{1,56,-5,33};
Arrays.sort (Intarray);
System.out.println (arrays.tostring (Intarray));
2> string sorting (uppercase after lowercase):
string[] Strarray = new string[]{"Z", "a", "D"};
Arrays.sort (Strarray);
System.out.println (arrays.tostring (Strarray));
3> String Alphabetic order (ignore case)
Arrays.sort (Strarray, String.case_insensitive_order);
4> Reverse Sorting
Arrays.sort (Strarray, Collections.reverseorder ());
Description
1>arrays.sort () uses a "fast-tuned sorting method
2> such as int[],double[],char[] arrays of cardinality types, the arrays class simply provides the default ascending arrangement, and does not provide the corresponding descending arrangement method
2. Sort the array of objects
To implement the ordering of an array of objects, you first implement the comparable or comparator interface. Here's a quick look at these two interfaces and their differences:
1> comparable is a sort interface, and if a class implements the comparable interface, it means "this class supports sorting." The class that implements the comparable interface supports sorting, assuming that there is now a list (or array) of objects for the class that implements the comparable interface, the list (or array) can be passed through Collections.sort (or Arrays.sort) to sort.
The comparable interface only includes a function, which is defined as follows:
Package Java.lang;
Import java.util.*;
Public interface Comparable<t> {
public int compareTo (T o);
}
Description
Suppose we use X.compareto (y) to "compare the size of x and Y". Returning a "negative number" means "x is smaller than Y"; returning "0" means "x equals Y"; returning "positive" means "X is greater than Y".
2> Comparator is the comparator interface, and if we need to control the order of a class, which itself does not support sorting (that is, not implementing the comparable interface), then we can create a "comparer for that class" to sort. This "comparer" only needs to implement the comparator interface.
The Comparator interface consists of only two functions, which are defined as follows:
Package java.util;
Public interface comparator<t> {
int compare (t O1, T O2);
Boolean equals (Object obj);
}
Description:
(1) If a class implements the comparator interface: it must implement the CompareTo (T o1,t O2) function, but it may not implement the Equals (Object obj) function.
Why not implement the Equals (Object obj) function? Because any class, the default is already implemented equals (Object obj). All classes in Java inherit from Java.lang.Object and implement the Equals (Object obj) function in Object.java, so all other classes are equivalent to implementing the function. The
(2) int compare (t O1, T O2) is the size of the comparison O1 and O2. Returning "negative numbers" means "O1 is smaller than O2"; returning "0" means "O1 equals O2"; returning "positive" means "O1 is greater than O2."
(3) This sorting algorithm is a "tuned merge sort" algorithm
we can easily find: comparable is equivalent to "internal comparator", and comparator is equivalent to "external comparator".
The following midterm exam questions to achieve the implementation of two interfaces:
Keyboard input two integer m,n. M as a random number of seeds, to generate n students of the high number, physics, English scores (scores are integers, greater than or equal to 0 less than or equal to 100), the school number starting from 1 in order to increase to N. Please sort the output by the total score from low to high.
Import Java.util.Comparator;
Import Java.util.Random;
Import Java.util.Scanner;
/** * Created by hexing on 15-5-20.
*//Implement comparable interface class STUDENT1 implements comparable<student1>{public int id,grad1, grad2, grad3,sum;
public int compareTo (Student1 o) {if (this.sum>o.sum) {return 1;
}else if (this.sum<o.sum) return-1;
else return 0;
}//Implement Comparator interface class Student2 implements comparator<student2>{public int id,grad1, grad2, grad3,sum;
public int Compare (Student2 O1, Student2 O2) {return o1.sum-o2.sum;
} public class Test1 {public static void main (string[] args) {int m, n;
System.out.println ("Please enter m,n:");
Scanner reader = new Scanner (system.in);
m = Reader.nextint ();
n = reader.nextint ();
Random r = new Random (m);
student1[] S1 = new Student1[n]; for (int i = 0; i < n; i++) {S1[i] =New Student1 ();
S1[i].id = i+1;
S1[i].grad1 = R.nextint (101);
S1[i].grad2 = R.nextint (101);
S1[i].grad3 = R.nextint (101);
S1[i].sum = S1[i].grad1+s1[i].grad2+s1[i].grad3;
} arrays.sort (S1);
System.out.println ("Sort of comparable interface implementations:"); for (int i = 0; i < s1.length i++) {System.out.printf ("%5d%5d%5d%5d%5d\n", s1[i].id,s1[i].grad1,s1[i].grad2
, s1[i].grad3,s1[i].sum);
} student2[] s = new Student2[n];
for (int i = 0; i < n; i++) {s[i] = new Student2 ();
S[i].id = i+1;
S[i].grad1 = R.nextint (101);
S[i].grad2 = R.nextint (101);
S[i].grad3 = R.nextint (101);
S[i].sum = S[i].grad1+s[i].grad2+s[i].grad3;
} arrays.sort (S, New Student2 ());
System.out.println ("Sort of comparator interface implementations:"); for (int i = 0; i < s.length i++) {System.out.printf ("%5d%5d%5d%5d%5d\n", s[I].id,s[i].grad1,s[i].grad2,s[i].grad3,s[i].sum);
}
}
}