Sorting of Strings

Aim:
To write a java program to sort a list of names in ascending order.

Description:
 First read number of strings. Accept number of strings into an array. Compare first string with remaining strings using String class compareTo () method. compareTo () method compares the given string in dictionary fashion. If the first string is big then by taking a temporary variable swap the two strings, continue this process till the end of the strings. The given strings will be in ascending sorted order.

Program:
import java.io.*;
class NameSort
{ public static void main(String args[ ]) throws IOException
{ BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
System.out.println("Enter how many names you want");
int n=Integer.parseInt(br.readLine() );
String a[ ]=new String[n];
int i,j;
System.out.println("Enter " + n + " names : ");
for(i=0;i<n;i++)
a[i]=br.readLine();
for(i=0;i<n;i++)
{ for(j=i+1;j<n ;j++)
{ if(a[i].compareTo(a[j])>0)
{ String temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.println("After sorting the names are");
for(i=0;i<n;i++)
System.out.println(a[i]);
}
}