System.out.print ("before sort:");
for (int i = 0; i< arr.length; i++)
System.out.print (Arr[i] + "");
System.out.println ();
Arrays.sort (arr);
System.out.print ("after sort:");
for (int i = 0; i< arr.length; i++)
System.out.print (Arr[i] + "");
System.out.println ();
Output results:
Before Sort:2 3 1 10 7 4
After Sort:1 2 3 4 7 10
We see that the sort results are sorted in ascending order, as is the order below.
Sorting of objects
objects can be placed in an array, the same call to Arrays.sort (object[] arr) can be placed in the set, with the Java.util.Collections sort (list list).
But this class must implement the Java.lang.Comparable interface. This interface has only one method: int Compartto (Object o),
Returns a positive integer when this object is larger than the object passed in. Take class programmer as an example:
Class Programmer implements Comparable{
private String name;
Private String language;
private double pay;
Public Programmer (string name, String language, double pay) {
THIS.name = name;
This.language = language;
This.pay = pay;
}
public int compareTo (Object o) {
Programmer other = (programmer) o;
return (int) pay-(int) Other.pay;
}
Public String toString () {
Return ' {name: ' + name + ', language: "+ language +", Money: "+ Pay +"} ";
}
}