Java Sort String Array - Java Language Basics

Java examples for Language Basics:String

Description

Java Sort String Array

Demo Code


import java.util.Arrays;
 
public class Main {
       //from  ww w. j  av a2s  . com
        public static void main(String args[]){
                String[] strNames = new String[]{"John", "alex", "Chris", "williams", "Mark", "Bob"};

                Arrays.sort(strNames);
               
                System.out.println("String array sorted (case sensitive)");
               
                for(int i=0; i < strNames.length; i++){
                        System.out.println(strNames[i]);
                }
               
                Arrays.sort(strNames);
               
                System.out.println("String array sorted (case insensitive)");

                for(int i=0; i < strNames.length; i++){
                        System.out.println(strNames[i]);
                }
 
        }
}
 

Result


Related Tutorials