Sorting Arrays in java



User Rating:  / 0
PoorBest 
Details
Category: Java
Created on Saturday, 28 July 2012 11:05
Last Updated on Saturday, 28 July 2012 11:05
Published on Saturday, 28 July 2012 11:05
Written by Administrator
Hits: 5642

Simple example for sorting Arrays in java :

View source
package sortexample;
 
import java.util.Arrays;
import java.util.Collections;
 
/**
 *
 * @author www.developerpages.gr
 */
public class SortExample {
 
    public static void main(String[] args) {
 
        int[] intArray = new int[] {413-23};
        
        System.out.println("Sort integers");
        System.out.println("-------------");
        Arrays.sort(intArray);
 
        for (int i = 0; i<intArray.length;i++)
        {  
            System.out.println(intArray[i]);
        }
        
        System.out.println("Sort Strings");
        System.out.println("------------");
        
        String[] strArray = new String[] {"z""a""C"};
        Arrays.sort(strArray);
 
        for (int i = 0; i<strArray.length;i++){
             System.out.println(strArray[i]);
        }
 
        System.out.println("Case-insensitive sort");
        System.out.println("---------------------");
        
        // Case-insensitive sort
        Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);
 
        for (int i = 0; i<strArray.length;i++){
             System.out.println(strArray[i]);
        }
 
        System.out.println("Reverse-order sort");
        System.out.println("------------------");
        // Reverse-order sort
        Arrays.sort(strArray, Collections.reverseOrder());
        for (int i = 0; i<strArray.length;i++){
             System.out.println(strArray[i]);
        }
        System.out.println("Case-insensitive reverse-order sort");
        System.out.println("-----------------------------------");
        
        // Case-insensitive reverse-order sort
        Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);
        Collections.reverse(Arrays.asList(strArray));
 
        for (int i = 0; i<strArray.length;i++){
             System.out.println(strArray[i]);
        }
        
    }
 
}