Sample Application: Array Sort
This function implements array sorting. Arrays can be of different types. Here an int array and a string array are being sorted. The procedure for sorting for two arrays are same, the only difference between two functions are the parameters are different one is an array of integer values and the other is an array of string values.
This is a simple function to implement array sorting.
// -----------------------------------------------------------------------------
// ArraySort.java
// -----------------------------------------------------------------------------
import java.util.Arrays;
public class ArraySort {
/**
* ------------------------------------------------------------------------------
* This function will sort an int array and produces the output
*
* @param array[] matches the array to be sorted
*
* ------------------------------------------------------------------------------
**/
public static void arraySort(int[] array) {
//Sorts the specified array of ints into ascending numerical order.
Arrays.sort(array);
//Prints the sorted array
for (int i = 0; i < array.length; i++)
System.out.print(array[i]+"\t");
System.out.println();
}
/**
* ------------------------------------------------------------------------------
* This function will sort a string array and produces the output
*
* @param array[] matches the array to be sorted
*
* ------------------------------------------------------------------------------
**/
public static void arraySort(String[] array) {
//Sorts the specified array of words in alphabetical order
Arrays.sort(array);
//Prints the sorted array
for (int i = 0; i < array.length; i++)
System.out.print(array[i]+"\t");
System.out.println();
}
}
ArraySort.java as its name implies will sort the given array and displays the output.
[Page 1] [Page 2]