Search Java Programs

Tuesday, January 26, 2010

How to Sort of Array integer and String

import java.util.Arrays;

/**
 *
 * @author javadb.com
 */
public class testing {

    /**
     * Example method for sorting an int array
     */
    public void sortIntArray() {

        int[] arrayToSort = new int[] {48, 5, 89, 80, 81, 23, 45, 16, 2};

        Arrays.sort(arrayToSort);

        for (int i = 0; i < arrayToSort.length; i++)
        {
            System.out.print(arrayToSort[i] + " ");
        }
        System.out.println("\n");
    }
    

    /**
     * Example method for sorting a String array
     */
    public void sortStringArray() {

        String[] arrayToSort = new String[] {"Oscar", "Charlie", "Ryan", "Adam", "David"};

        Arrays.sort(arrayToSort);

        for (int i = 0; i < arrayToSort.length; i++)
        {
            System.out.print(arrayToSort[i] + " ");
        
        }
        System.out.println("\n");
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        testing main = new testing();
        main.sortIntArray();
        main.sortStringArray();
    }
}

No comments:

Post a Comment

Website Design by Mayuri Multimedia