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

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 = {15, 10, 7, 13, 9};
      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 = {"Tom", "Ed", "Al", "Tim"};
      Arrays.sort(people);    //use the built-in sorting routine
      for (int i = 0; i < 4; i++)
         System.out.println(people[ i ]);
   }
}

 

Output:

7
9
10
13
15
Al
Ed
Tim
Tom

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  | Java Resource CD