Sorting of arrays and sets in Java
Two types of objects to be sorted: array and set
1. Set
Java. util. Collections class provides the sorting method sort ();
static void |
sort(List list)
Sorts the specified list into ascending order, according toNatural orderingOf its elements. |
static void |
sort(List list, Comparator c)
Sorts the specified list according to the order induced by the specified comparator. |
Two sorting methods:
1) Implement the java. Lang. Comparable interface of the sorted object, and determine the sorting rules in the compareto () method.
2) use the java. util. comparator instance to determine the sorting rule.
2. Array
The Java. util. arrays class provides various overloaded sorting methods sort ();
static void |
sort(byte[] a)
Sorts the specified array of bytes into ascending numerical order. |
static void |
sort(byte[] a, int fromIndex, int toIndex)
Sorts the specified range of the specified array of bytes into ascending numerical order. |
static void |
sort(char[] a)
Sorts the specified array of chars into ascending numerical order. |
static void |
sort(char[] a, int fromIndex, int toIndex)
Sorts the specified range of the specified array of chars into ascending numerical order. |
static void |
sort(double[] a)
Sorts the specified array of doubles into ascending numerical order. |
static void |
sort(double[] a, int fromIndex, int toIndex)
Sorts the specified range of the specified array of doubles into ascending numerical order. |
static void |
sort(float[] a)
Sorts the specified array of floats into ascending numerical order. |
static void |
sort(float[] a, int fromIndex, int toIndex)
Sorts the specified range of the specified array of floats into ascending numerical order. |
static void |
sort(int[] a)
Sorts the specified array of ints into ascending numerical order. |
static void |
sort(int[] a, int fromIndex, int toIndex)
Sorts the specified range of the specified array of ints into ascending numerical order. |
static void |
sort(long[] a)
Sorts the specified array of longs into ascending numerical order. |
static void |
sort(long[] a, int fromIndex, int toIndex)
Sorts the specified range of the specified array of longs into ascending numerical order. |
static void |
sort(Object[] a)
Sorts the specified array of objects into ascending order, according toNatural orderingOf its elements. |
static void |
sort(Object[] a, Comparator c)
Sorts the specified array of objects according to the order induced by the specified comparator. |
static void |
sort(Object[] a, int fromIndex, int toIndex)
Sorts the specified range of the specified array of objects into ascending order, according toNatural orderingOf its elements. |
static void |
sort(Object[] a, int fromIndex, int toIndex, Comparator c)
Sorts the specified range of the specified array of objects according to the order induced by the specified comparator. |
static void |
sort(short[] a)
Sorts the specified array of shorts into ascending numerical order. |
static void |
sort(short[] a, int fromIndex, int toIndex)
Sorts the specified range of the specified array of shorts into ascending numerical order. |
If int [] and other types can be directly sorted in ascending order
If you want to sort the object [] type, you need to determine the order. There are still two methods. The sorted class implements the comparable interface or provides the java. util. comparator class.
|
/*
* Created on 2005-11-27
*
* TODO To change the template for this generated file go
* Window-Preferences-Java-Code Style-Code Templates
*/
Package sort;
Import java. util. ArrayList; Import java. util. Comparator;
Import java. util. Arrays;
Import java. util. Collections;
Import java. util. Iterator;
// Import java. util. Random;
Import java. lang. Math;
/**
* @ Author Jonathan
*
* <B> sorting method summary </B>
* TODO To change the template for this generated type comment go
* Window-Preferences-Java-Code Style-Code Templates
*/
Public class SortCollection {
Private void sortByComparableOfList (){
ArrayList al = new ArrayList ();
For (int I = 0; I <10; I ++ ){
Int tmp = (int) (Math. random () * 10 );
Al. add (new SortedObjectOne (tmp, "List." + I ));
}
Collections. sort (al );
Iterator itt = al. iterator ();
While (itt. hasNext ())
System. out. println (itt. next ());
}
Private void sortByComparatorOfList (){
ArrayList al = new ArrayList ();
For (int I = 0; I <10; I ++ ){
Int tmp = (int) (Math. random () * 10 );
Al. add (new SortedObjectOne (tmp, "List." + I ));
}
Collections. sort (al, new SortOrderAsc ());
Iterator itt = al. iterator ();
While (itt. hasNext ())
System. out. println (itt. next ());
}
/** Default sorting of integer arrays */
Private void sortByDefaultOfArray (){
Int [] array = new int [10];
For (int I = 0; I <array. length; I ++ ){
Array [I] = (int) (Math. random () * 10 );
}
Arrays. sort (array );
For (int I = 0; I <array. length; I ++ ){
System. out. println ("<Array" + I + ">:<" + array [I] + "> ");
}
}
/** Default sorting of object arrays */
Private void sortByComparableOfArray (){
SortedObjectOne [] array = new SortedObjectOne [10];
For (int I = 0; I <array. length; I ++ ){
Array [I] = new SortedObjectOne (int) (Math. random () * 10), "Array" + I );
}
Arrays. sort (array );
For (int I = 0; I <array. length; I ++ ){
System. out. println (array [I]);
}
}
/** Custom sorting of object arrays */
Private void sortByComparatorOfArray (){
SortedObjectOne [] array = new SortedObjectOne [10];
For (int I = 0; I <array. length; I ++ ){
Array [I] = new SortedObjectOne (int) (Math. random () * 10), "Array" + I );
}
Arrays. sort (array, new SortOrderAsc ());
For (int I = 0; I <array. length; I ++ ){
System. out. println (array [I]);
}
}
Public static void main (String args []) {
SortCollection SC = new SortCollection ();
System. out. println ("-- sort list by comparable --");
SC. sortByComparableOfList ();
System. out. println ("-- sort list by comparator --");
SC. sortByComparatorOfList ();
System. out. println ("-- sort array by default --");
SC. sortByDefaultOfArray ();
System. out. println ("-- sort array by comparable --");
SC. sortByComparableOfArray ();
System. out. println ("-- sort array by comparator --");
SC. sortByComparatorOfArray ();
}
}
/** Determine the sorting order based on the Comparable interface */
Class SortedObjectOne implementsComparable{
Public int attr1;
Public String attr2;
Public SortedObjectOne (int attr1, String attr2 ){
This. attr1 = attr1;
This. attr2 = attr2;
}
/* (Non-Javadoc)
* @ See java. lang. Comparable # compareTo (java. lang. Object)
*/
Public intCompareTo(Object arg0 ){
// TODO Auto-generated method stub
If (arg0 instanceof SortedObjectOne ){
SortedObjectOne obj = (SortedObjectOne) arg0;
Return this. attr1-obj. attr1;
}
Return 0;
}
/* (Non-Javadoc)
* @ See java. lang. Object # toString ()
*/
Public String toString (){
// TODO Auto-generated method stub
Return "<" + this. attr2 + ">:<" + this. attr1 + "> ";
}
}
/** Determine the sorting order based on Comparator */
Class SortOrderAsc implements Comparator {
/* (Non-Javadoc)
* @ See java. util. Comparator # compare (java. lang. Object, java. lang. Object)
*/
Public int compare (Object arg0, Object arg1 ){
// TODO Auto-generated method stub
If (arg0 instanceof SortedObjectOne & arg1 instanceof SortedObjectOne ){
SortedObjectOne tmp0 = (SortedObjectOne) arg0;
SortedObjectOne tmp1 = (SortedObjectOne) arg1;
Return tmp1.attr1-tmp0.attr1;
}
Return 0;
}
} |
Program Execution result:
-- Sort list by comparable --
<List.3>: <0>
<List.9>: <0>
<List.0>: <1>
<List.1>: <1>
<List.4>: <1>
<List.6>: <3>
<List.8>: <3>
<List.5>: <7>
<List.2>: <8>
<List.7>: <9>
-- Sort list by comparator --
<List.7>: <5>
<List.1 >:< 4>
<List.2>: <3>
<List.8>: <3>
<List.3>: <2>
<List.5>: <2>
<List.9>: <2>
<List.0>: <0>
<List.4>: <0>
<List.6>: <0>
-- Sort array by default --
<Array0>: <1>
<Array1>: <2>
<Array2>: <2>
<Array3>: <6>
<Array4>: <7>
<Array5>: <8>
<Array6>: <8>
<Array7>: <9>
<Array8>: <9>
<Array9>: <9>
-- Sort array by comparable --
<Array2>: <0>
<Array1>: <1>
<Array7>: <1>
<Array9>: <3>
<Array0>: <4>
<Array8>: <4>
<Array5>: <5>
<Array6>: <5>
<Array3>: <8>
<Array4>: <9>
-- Sort array by comparator --
<Array3>: <9>
<Array7>: <9>
<Array1>: <7>
<Array4>: <5>
<Array5>: <3>
<Array8>: <3>
<Array0>: <2>
<Array2>: <1>
<Array6>: <1>
<Array9>: <0> |