No.
Arrays.sort()
The class Arrays
contains many sorting methods that
you can use instead of writing your own.
The class is part of the java.util
package.
The sorting methods use algorithms that are more sophisticated than the
simple sorts described above and so are much faster.
Here are some of the sorting methods from Arrays
.
All of them sort their array into ascending order:
static void sort(byte[] a) static void sort(char[] a) static void sort(double[] a) static void sort(float[] a) static void sort(int[] a) static void sort(long[] a) static void sort(Object[] a) static void sort(short[] a)
Here is a program that sorts an array of int
import java.util.*; public class ArraySortTester { public static void main ( String[] args ) { int[] values = { 17, 5, 21, 8, 19, 2, 23, 15, 4, 13 }; // print out the array System.out.println("initial values: "); for ( int val : values ) System.out.print( val + ", " ); // sort the array Arrays.sort( values ); // NOTE // print out the array System.out.println("\n\nsorted values: "); for ( int val : values ) System.out.print( val + ", " ); System.out.println( ); } }
Here is a run of the program:
C:\JavaSource>java ArraySortTester initial values: 17, 5, 21, 8, 19, 2, 23, 15, 4, 13, sorted values: 2, 4, 5, 8, 13, 15, 17, 19, 21, 23,
Is there a Arrays.sort()
method for each type of Java primitive?