Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Sorting an arrayList

807580May 19 2010 — edited May 19 2010
Hi,
I am trying to sort an arrayList which has numbers as values.

Data in arrayList: 9, 8, 70, 7, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 6, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 5, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 4, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 3, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 2, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 1

<code>
package test;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.Collections;

public class ReadLine {

/**
* @param args
*/
public static void main(String[] args)
{
String test="9, 8, 70, 7, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 6, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 5, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 4, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 3, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 2, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 1";
ArrayList<String> arrayList=new ArrayList<String>();
StringTokenizer stringTokenizer=new StringTokenizer(test);
while (stringTokenizer.hasMoreElements())
{
arrayList.add(stringTokenizer.nextToken(","));
}
Collections.sort(arrayList);
System.out.println(arrayList);
}
}

</code>
The above code sorts the arraylist but the output is:
1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 3, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 4, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 5, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 6, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 7, 70, 8, 9

which should have been rather like 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 and so on....


Thanks in advance
regards
Prashanth
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 16 2010
Added on May 19 2010
4 comments
72 views