14) Write a program in Java to accept name of cities from the user and sort them in ascending order (Use Command Line Arguments)


import java.io.*;
class CitySort
{
public static void main (String args[]) throws IOException
{
int n,len,len1,c,i,j,k=0,m=0;
String str[]=new String[5] ;
String s1;
String s2,temp;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for (i=0;i<5;i++)
{
System.out.println(“Enter the string “+i+” of the Array :=> “);
str[i]= br.readLine();
}

System.out.println(“\nSorted Strings are :-”);
for(i=0;i<5;i++)
{

for(j=i+1;j<5;j++)
{
if(str[j].compareTo(str[i])<0)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
}
System.out.println(str[i]);
}
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s