Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Ascending Order Swap String


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

Your rating: None

R u sure you are getting the

R u sure you are getting the results in the descending order if you run the above program??
I am getting the results in ascending order.
To get the results in the other order change the comparison sign to ">" in the if block if (name[j].compareTo(name[i])>0) .

Yes. I am getting them in

Yes. I am getting them in descending order:

Here is the Result with
if (name[j].compareTo(name[i])<0)

C:\jwork>java swapstr
Patna
Mumbai
Delhi
Chennai
Andra
Agra

C:\jwork>javac swapstr.java

Here the Result with
if (name[j].compareTo(name[i])>0)

C:\jwork>java swapstr
Patna
Mumbai
Delhi
Chennai
Andra
Agra

C:\jwork>

I am seeing the changes

Just to make sure that you are executing the updated program, put a print with some text.

You can just use

You can just use Arrays.sort(names) to sort ascending but why are you working so hard to sort descending? Java has a Comparator interface that works pretty well.

import java.util.Arrays;
import java.util.Comparator;


public class SortNamesArray {

public static void main(String[] args) {

String[] names = {"Chenni", "Mumbai", "Delhi","Agra","Andhra","Patna"};

// Ascending
System.out.println("Ascending");
System.out.println("---------");
Arrays.sort(names);
printNames(names);

// Descending
System.out.println("Descending");
System.out.println("----------");
Comparator<String> descending = new DescendingComparator();
Arrays.sort(names, descending);
printNames(names);
}

private static void printNames(String[] names) {
for(String name: names) {
System.out.println(name);
}
}
}

class DescendingComparator implements Comparator<String>
{
@Override
public int compare(String str1, String str2) {
// TODO Auto-generated method stub
return str2.compareTo(str1);
}
}

Here is code for the problem

Hiyou can use this code for swaping strings in ascending order.

import java.util.Arrays;
import java.util.Collections;

public class ArrayAscSort {

/**
* @param args
*/
public static void main(String[] args) {

String[] names = {"Chennai","Mumbai","Delhi","Agra","Andhra","Patna"};

Collections.sort(Arrays.asList(names));

for(String name : names){
System.out.println(name + "\n");
}
}
}

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <p> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br /> <br> <strike>
  • Lines and paragraphs break automatically.
  • Use <!--pagebreak--> to create page breaks.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
Just checking to see if you're an actual person rather than a spammer. Sorry for the inconvenience.