Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to sort an array of Strings using compareTo(). This is my code:

static  String Array[]={" Hello " , " This " , "is ", "Sorting ", "Example"};
String  temp; 

public static void main(String[] args)

{    

 for(int j=0; j<Array.length;j++)
 {
     for (int i=j+1 ; i<Array.length; i++)
     {
         if(Array[i].compareTo(Array[j])<0)
         {
             String temp= Array[j];
             Array[j]= Array[i]; 
             Array[i]=temp;


         }
     }

     System.out.print(Array[j]);
 }
}

now output is : Hello This Example Sorting is

I am getting some result but not the one i want to get , for me logic is right so whats wrong here , anyone can help me please?

share|improve this question
You should really work on your code convention. – user238033 Oct 20 '12 at 8:03
yes i will , infect i m new to java . and was totally stuck with it , had no idea about such minor issues .. – Sikander Oct 20 '12 at 9:36
A little bit advice I can give you when programming is stay consistent with convention. If you're wrong about one thing, do that one thing wrong everywhere instead of doing it right some places and wrong other places. – user238033 Oct 20 '12 at 22:31

5 Answers

up vote 5 down vote accepted

Your output is correct. Denote the white characters of " Hello" and " This" at the beginning.

Another issue is with your methodology, why don't you use Arrays.sort() method?

String[] strings = { " Hello ", " This ", "is ", "Sorting ", "Example" };
Arrays.sort(strings);
share|improve this answer
Thanks ..such foolish mistake , i was really stuck – Sikander Oct 20 '12 at 7:45

Instead of this line

if(Array[i].compareTo(Array[j])<0)

use this line

if(Array[i].trim().compareTo(Array[j].trim())<0)

and you are good to go. The reason your current code is not working is explained by other users already. This above replacement is one workaround amongst several that you could apply.

share|improve this answer
Thanks for reply , it really helped . – Sikander Oct 20 '12 at 8:57

You are having leading space in Hello and this. Try removing them and it will work.

share|improve this answer

Apart from leading spaces, your logic works fine. Just for your information, java provides built in api for sorting,

java.util.Arrays.sort(arrays);
share|improve this answer

Apart from the alternative solutions that were posted here (which are correct), no one has actually answered your question by addressing what was wrong with your code.

It seems as though you were trying to implement a selection sort algorithm. I will not go into the details of how sorting works here, but I have included a few links for your reference =)

Your code was syntactically correct, but logically wrong. You were partially sorting your strings by only comparing each string with the strings that came after it. Here is a corrected version (I retained as much of your original code to illustrate what was "wrong" with it):

static  String Array[]={" Hello " , " This " , "is ", "Sorting ", "Example"};
String  temp;

//Keeps track of the smallest string's index
int  shortestStringIndex; 

public static void main(String[] args)  
{              

 //I reduced the upper bound from Array.length to (Array.length - 1)
 for(int j=0; j < Array.length - 1;j++)
 {
     shortestStringIndex = j;

     for (int i=j+1 ; i<Array.length; i++)
     {
         //We keep track of the index to the smallest string
         if(Array[i].trim().compareTo(Array[shortestStringIndex].trim())<0)
         {
             shortestStringIndex = i;  
         }
     }
     //We only swap with the smallest string
     if(shortestStringIndex != j)
     {
         String temp = Array[j];
         Array[j] = Array[shortestStringIndex]; 
         Array[shortestStringIndex] = temp;
     }
 }
}

Further Reading

The problem with this approach is that its asymptotic complexity is O(n^2). In simplified words, it gets very slow as the size of the array grows (approaches infinity). You may want to read about better ways to sort data, such as quicksort.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.