Today, I am working on an update-based method to search for all the files in a directory. Because it takes more time to write algorithms, I simply use the sort () method provided by arrays provided by j2se, this is more effort-saving than the limit. For basic data types, only arrays. Sort (array) ["NOTE: arrays are arrays declared as basic data types, such as int []"]
For object types, implementComparableSo you have to overload the compareto () method. With this methodArrays. Sort() Can be sorted according to the return value of this method. In fact, the basic data types also have implementComparableSo it can be used in this way. In general, the sorting result we are used to is from small to big. Therefore, in compareto (), "greater than" indicates the return positive value. Suppose that the expected result is from large to small, you only need to change the return part. It is not recommended to do this, it will reduce the readability of the program. Besides, just reverse the array and do not get the same result...
Import java. util. arrays;
Import java. util. comparator;
Public class arraysortdemo {
/**
* Sorting of integer Arrays
*/
Public void sortintarray (){
Int [] arraytosort = new int [] {48, 5, 89, 80, 81, 23, 45, 16, 2 };
System. Out. println ("sorting by integer array, before sorting :");
For (INT I = 0; I <arraytosort. length; I ++ ){
System. Out. Print (arraytosort [I] + ",");
}
// Call the static sorting method sort of the array
Arrays. Sort (arraytosort );
System. Out. println ();
System. Out. println ("sorted :");
For (INT I = 0; I <arraytosort. length; I ++ ){
System. Out. Print (arraytosort [I] + ",");
}
}
/**
* Sort array sorting demo
*/
Public void sortstringarray (){
String [] arraytosort = new string [] {"Oscar", "Charlie", "Ryan", "Adam", "David", "aff", "aff "};
System. Out. println ();
System. Out. println ("Sort array sorting, before sorting :");
For (INT I = 0; I <arraytosort. length; I ++ ){
System. Out. Print (arraytosort [I] + ",");
}
System. Out. println ();
System. Out. println ("sorted :");
// Call the static sorting method sort of the array
Arrays. Sort (arraytosort );
For (INT I = 0; I <arraytosort. length; I ++ ){
System. Out. Print (arraytosort [I] + ",");
}
}
/**
* Object array sorting demo
*/
Public void sortobjectarray (){
Dog O1 = new dog ("dog1", 1 );
Dog O2 = new dog ("dog2", 4 );
Dog O3 = new dog ("dog3", 5 );
Dog O4 = new dog ("dog4", 2 );
Dog O5 = new dog ("dog5", 3 );
DOG [] dogs = new dog [] {O1, O2, O3, O4, O5 };
System. Out. println ();
System. Out. println ("Before sorting object arrays :");
For (INT I = 0; I <dogs. length; I ++ ){
Dog dog = dogs [I];
System. Out. Print (dog. getname () + "[" + dog. getweight () + "],");
}
Arrays. Sort (dogs, new byweightcomparator ());
System. Out. println ();
System. Out. println ("sorted :");
For (INT I = 0; I <dogs. length; I ++ ){
Dog dog = dogs [I];
System. Out. Print (dog. getname () + "[" + dog. getweight () + "],");
}
}
Public static void main (string [] ARGs ){
Arraysortdemo T = new arraysortdemo ();
T. sortintarray ();
T. sortstringarray ();
T. sortobjectarray ();
}
}
/**
* Defines a dog class.
*/
Class dog {
Private string name;
Private int weight;
Public dog (string name, int weight ){
This. setname (name );
This. Weight = weight;
}
Public int getweight (){
Return weight;
}
Public void setweight (INT weight ){
This. Weight = weight;
}
Public void setname (string name ){
This. Name = Name;
}
Public String getname (){
Return name;
}
}
/**
* To sort objects, You need to implement the compare (T O1, t O2) method of Java. util. comparator interface, and define the Sorting Algorithm yourself in this method.
*/
Class byweightcomparator implements comparator {
Public final int compare (Object pfirst, object partition Cond ){
Int afirstweight = (DOG) pfirst). getweight ();
Int asecondweight = (DOG) specify Cond). getweight ();
Int diff = afirstweight-asecondweight;
If (diff> 0)
Return 1;
If (diff <0)
Return-1;
Else
Return 0;
}
}
Execution result:
Sort integer arrays. Before sorting:
, 2
After sorting:
, 80, 81, 89
Sort array sorting, before sorting:
Oscar, Charlie, Ryan, Adam, David, aff, aff
After sorting:
Adam, aff, Charlie, David, Oscar, Ryan, aff
Before sorting object arrays:
Dog1 [1], dog2 [4], dog3 [5], dog4 [2], dog5 [3]
After sorting:
Dog1 [1], dog4 [2], dog5 [3], dog2 [4], dog3 [5]
Arrays. Sort ()