10 September, 2012

Sort the Name List

import java.util.*;
class Sortst
{
public static void main(String a[])
{
System.out.println("How many string u want to enter");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
String st[]=new String[n+1];
for(int i=0;i<n;i++)
{
System.out.println("Enter string "+(i+1));
st[i]=s.next();
}
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(st[i].compareTo(st[j])>0)
{
String t=st[i];
st[i]=st[j];
st[j]=t;
}
}
}
System.out.println("Strings in order are :\n\n");
for(int i=0;i<n;i++)
System.out.println(st[i]);
}
}

No comments:

Post a Comment

Thank You !