|
|
Hi Friends,
Here is the code to swap the strings in an array in the descending order. Can you help me to tweak this code to swap the strings in the ascending order? Thanks for your help in advance!
class swapstr
{
static String name[]={"Chennai","Mumbai","Delhi","Agra","Andhra","Patna"};
public static void main(String args[])
{
int size = name.length;
String temp = null;
for (int i=0; i<size;i++)
{
for (int j=i+1;j<size;j++)
{
if (name[j].compareTo(name[i])<0)
{
temp=name[i];
name[i]=name[j];
name[j]=temp;
}
}
}
for (int i=0;i<size;i++)
{
System.out.println(name[i]);
}
}
}Result:
C:\jwork>java swapstr
Patna
Mumbai
Delhi
Chennai
Andra
Agra
C:\jwork>
I am expecting the result to be:
Agra
Andra
Chennai
Delhi
Mumbai
Patna