Google Search

Monday, October 10, 2011

Write a Java program for sorting a given list of names in ascending order.


Write a Java program for sorting a given list of names in ascending order.


import java.lang.*;
import java.io.*;
class asc
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("enter how many strings");
int n=Integer.parseInt(br.readLine());
String x[]=new String[n];
System.out.print("enter "+n+" strings");
for(int i=0;i<n;i++)
{
x[i]=br.readLine();
}
String s=new String();
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(x[i].compareTo(x[j])<6)
{
s=x[i];
x[i]=x[j];
x[j]=s;
}
}
}
System.out.print("string in alphebetical order are");
for(int i=0;i<n;i++)
{
System.out.println(x[i]);
}
}
}

0 comments:

Post a Comment