by, 03-04-2012 at 11:26 AM (1109 Views)
To sort arrays, static methods are provided by the class java.util.Arrays. Kinds of arrays that are supported by it are as following:
i. Arrays of object types
ii. Arrays of primitive types
It is possible to apply the sorting method to range of arrays or some particular part. It is also applicable to the whole array.
For object type array comparators shall be provided so that to define it to such kind of sorting. Few Array methods are explained here along with their description:
- Arrays.sort(pa): Primitive type array’s elements are sorted to ascending order. This is usually done by the help of natural ordering.
- Arrays.sort(pa, from, to): Object type array elements are sorted to an ascending order. This is done by that order usage which is defined usually by the help of comparable interface. Ultimately the method thecompareTo is defined by it. It shall be noted that number of Java classes like BigInteger, String or Double etc would be implementing Comparable.
- Arrays.sort(oa, from, to): Array elements are sorted to an ascending order in range (from-to) of object type.
- Arrays.sort(oa, comp): Object type array’s elements are sorted to an ascending order. This is done by the help of Comparator comp.
- Arrays.sort(oa, from, to, comp): Array elements are sorted in ascending order, in range (from..to) of object type. This is also done by the help of Comparator comp.