Yes. So an array of String
references can be sorted.
Here is a program that demonstrates how sort()
is used
with an array of String
references.
The
import java.util.Arrays; class ArrayDemoTwo { public static void main ( String[] args ) { String[] animals = {"bat", "fox", "gnu", "eel", "ant", "dog", "fox", "gnat" }; System.out.print("Scrambled array: "); for ( int j=0; j < animals.length; j++ ) System.out.print( animals[j] + " "); System.out.println(); Arrays.sort( animals ); System.out.print("Sorted array: "); for ( int j=0; j < animals.length; j++ ) System.out.print( animals[j] + " "); System.out.println(); } }
The output of the program is:
Scrambled array: bat fox gnu eel ant dog fox gnat Sorted array: ant bat dog eel fox fox gnat gnu
What is required if sort()
is to be used with a
class that you define?