MathBits.com
Return to Unit Menu | Java Main Page | MathBits.com | Terms of Use  

Built-In Sorting Routines

There are often built-in sort "routines" in Java compiler packages, such as Arrays.sort ( ).  It is imperative that you, the programmer, know and understand at least one method of sorting an array, separate from a built in "routine", for test taking purposes. 


Example - Using the routine Arrays.sort( )

// short demo to show built-in sorting routine at work
import java.io.*;
import java.util.Arrays;  //notice the new required import statement
import BreezyGUI.*;
public class SortTest
{
   public static void main(String[] args)
   {
      //Sorting integers
      int[ ] nums = {12, 18, 7, 20, 11};
      Arrays.sort(nums);    //use the built-in sorting routine
      for (int i = 0; i < 5; i++)
         System.out.println(nums[ i ]);

      //Sorting strings
      String[ ] people = {"Wally", "Henry", "Felix", "George"};
      Arrays.sort(people);    //use the built-in sorting routine
      for (int i = 0; i < 4; i++)
         System.out.println(people[ i ]);
   }
}
yea guy

 

Output:

7
11
12
18
20
Felix
George
Henry
Wally

Notice that this built-in routine, as seen here, sorts into "ascending" order.


					


Return to Unit Menu | Java Main Page | MathBits.com | Terms of Use