# Problem with sorting by alphabetical order

• April 6th, 2012, 02:49 PM
pikapika
Problem with sorting by alphabetical order
Hello, I have started to learn java for about 3 months, thus I don't know much about it. I want to write a short code that can sort an array of name by alphabetical order. I did it in two different ways, the 1st one worked, but the second ways did not. Would you please help me to correct the problem. Thanks a lot.

This is the one that worked

Code :

```import java.util.*; public class testsortname{ public static void main (String args[]){   String s1= "Jerry";String s2= "AAA";String s3= "BTom";String s4= "AATom";String s5= "TDom"; String s6= "BlueBerry";String s7= "Samson";String s8= "Rinor";String s9= "Aladin";String s10= "FarFar"; String[] test=new String[10]; test[0]=s1;test[1]=s2;test[2]=s3;test[3]=s4;test[4]=s5; test[5]=s6;test[6]=s7;test[7]=s8;test[8]=s9;test[9]=s10; for(int j=0;j<test.length;j++){ System.out.println(test[j]);}   System.out.println(s1.compareTo(s2)); System.out.println(s2.compareTo(s3));   int i=1; String[] b=new String[test.length];b[0]=test[0];   while (i<test.length){ int k = i-1;boolean condition = false; while(k>=0&&condition==false){ if(b[k].compareTo(test[i])<=0){ b[k+1]=test[i];condition=true; } else{b[k+1]=b[k];b[k]=test[i];k--;} } i++;}   System.out.println("--------------------------------");   for(int j=0;j<b.length;j++){ System.out.println(b[j]); }   }   }```

This is the one that didn't

Code :

```import java.util.*; public class testsortname2{ public static void main (String args[]){   String s1= "Jerry";String s2= "AAA";String s3= "BTom";String s4= "AATom";String s5= "TDom"; String s6= "BlueBerry";String s7= "Samson";String s8= "Rinor";String s9= "Aladin";String s10= "FarFar"; String[] test=new String[10]; test[0]=s1;test[1]=s2;test[2]=s3;test[3]=s4;test[4]=s5; test[5]=s6;test[6]=s7;test[7]=s8;test[8]=s9;test[9]=s10;   for(int j=0;j<test.length;j++){ System.out.println(test[j]);}   String[] temp=new String[test.length]; temp[0]=test[0];   for(int i=1;i<10;i++){ int k=0; while(k<=i&&(test[i].compareTo(temp[k])>0)){k++;}   for(int j=10;j<=k+1;j--){temp[j]=temp[j-1];} temp[k]=test[i];   }   System.out.println("--------------------------------");   for(int j=0;j<temp.length;j++){ System.out.println(temp[j]);   }}}```
• April 6th, 2012, 05:09 PM
pbrockway2
Re: Problem with sorting by alphabetical order
Why - in what way - did the second version not work?

Did it not compile? If so, post the compiler's messages if you can't understand them and say which lines of your code they refer to.

Did it throw an exception at runtime? If so, post the complete stack trace (the long message you get when an exception happens) and, again, say what lines of your code it is referring to.

Did it show some other unwanted behaviour at runtime? If so, say what you expected would be output and what you actually observed.

-----

The code could be tidied up somewhat. Spaces (horizontal and new lines) cost nothing and they provide the eye with information about logical grouping. Blocks of code, especially, should be indented (and in a consistent manner). Consider descriptive variable names as these also carry with them an expression of your intent.
• April 6th, 2012, 05:12 PM
andreas90
Re: Problem with sorting by alphabetical order
Quote:

Originally Posted by pikapika
Hello, I have started to learn java for about 3 months, thus I don't know much about it. I want to write a short code that can sort an array of name by alphabetical order. I did it in two different ways, the 1st one worked, but the second ways did not. Would you please help me to correct the problem. Thanks a lot.

This is the one that didn't

Code :

```import java.util.*; public class testsortname2{ public static void main (String args[]){   String s1= "Jerry";String s2= "AAA";String s3= "BTom";String s4= "AATom";String s5= "TDom"; String s6= "BlueBerry";String s7= "Samson";String s8= "Rinor";String s9= "Aladin";String s10= "FarFar"; String[] test=new String[10]; test[0]=s1;test[1]=s2;test[2]=s3;test[3]=s4;test[4]=s5; test[5]=s6;test[6]=s7;test[7]=s8;test[8]=s9;test[9]=s10;   for(int j=0;j<test.length;j++){ System.out.println(test[j]);}   String[] temp=new String[test.length]; temp[0]=test[0];   for(int i=1;i<10;i++){ int k=0; while(k<=i&&(test[i].compareTo(temp[k])>0)){k++;}   for(int j=10;j<=k+1;j--){temp[j]=temp[j-1];} temp[k]=test[i];   }   System.out.println("--------------------------------");   for(int j=0;j<temp.length;j++){ System.out.println(temp[j]);   }}}```

Hello pikapika!
It would help if you explained what you got (errors, exceptions, unexpected behaviour) when you ran your program. But as i can see you will probably face a NullPointerException in
Code java:

```while (k <= i && (test[i].compareTo(temp[k]) > 0)) { k++; }```
because you only assign a value to temp[0] and at some point you will check temp[1], temp[2],....
Hope it helps.
• April 6th, 2012, 08:36 PM
pikapika
Re: Problem with sorting by alphabetical order
Quote:

Originally Posted by andreas90
Hello pikapika!
It would help if you explained what you got (errors, exceptions, unexpected behaviour) when you ran your program. But as i can see you will probably face a NullPointerException in
Code java:

```while (k <= i && (test[i].compareTo(temp[k]) > 0)) { k++; }```
because you only assign a value to temp[0] and at some point you will check temp[1], temp[2],....
Hope it helps.

Yes, that's the error I got, the NullPointerException and I did not understand what was going on. Thanks for trying to help

@ pbrockway2 : Thanks for trying to help

This is the error message:

Quote:

java.lang.NullPointerException
at java.lang.String.compareTo(Unknown Source)
at testsortname2.main(testsortname2.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:271)
• April 6th, 2012, 08:47 PM
Norm
Re: Problem with sorting by alphabetical order
Quote:

java.lang.NullPointerException
at java.lang.String.compareTo(Unknown Source)
at testsortname2.main(testsortname2.java:19)
what variable has a null value on line 19? If you can not see which one it is, add a println to print out the values of all the variables used on line 19. When you find the variable you need to look back in the code to see why that variable does not have a valid value.
• April 6th, 2012, 08:50 PM
pbrockway2
Re: Problem with sorting by alphabetical order
 Norm's answer is a better way to go...
• April 6th, 2012, 09:58 PM
pikapika
Re: Problem with sorting by alphabetical order
Thanks guys, I figure it out

Quote:

while(k<=i&&(test[i].compareTo(temp[k])>0)){k++;}

Quote:

for(int j=10;j<=k+1;j--){temp[j]=temp[j-1];}
temp[k]=test[i];
should be changed to

Quote:

while(k<i&&(test[i].compareTo(temp[k])>0)){k++;}
Quote:

for(int j=9;j>=k+1;j--){temp[j]=temp[j-1];}
temp[k]=test[i];
because there are 10 cells in the array, thus the max index is 9, and j should be greater than/ equal to k+1.