Sunday, August 12, 2012

Java program to perform String sorting


import java.io.*;
class stringsort
{
String s[];
int n;
stringsort(int q)
{
s=new String[q];
n=q;
}
void sortstring()throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter The names: ");
for(int i=0;i<s.length;i++){
System.out.print("name: "+(i+1));
s[i]=br.readLine();}
}
void sort()
{
for(int i=0;i<s.length-1;i++)
for(int j=i+1;j<s.length;j++)
{
if(s[i].compareTo(s[j])>0)
{
String temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
System.out.println("\nThe sorted array is: ");
for(int i=0;i<s.length;i++)
System.out.println(s[i]);
}
public static void main(String[] args)throws IOException
{
BufferedReader b= new BufferedReader(new InputStreamReader(System.in));
System.out.println("\nEnter no of names : " );
int x=Integer.parseInt(b.readLine());
stringsort s=new stringsort(x);
s.sortstring();
s.sort();
}
}

No comments:

Post a Comment