File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Need Help - Sorting an ArrayList of Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need Help - Sorting an ArrayList of Strings" Watch "Need Help - Sorting an ArrayList of Strings" New topic
Author

Need Help - Sorting an ArrayList of Strings

K Bala
Greenhorn

Joined: Jul 14, 2005
Posts: 28
I have an ArrayList of Strings like this

"132 8"
"132 12"
"132 4"
"132 19"

When I sort it using Collections.sort() method, I get

132 12
132 19
132 4
132 8

I want the ArrayList to be sorted like this

132 4
132 8
132 12
132 19

How can I achieve this? Please help


Quitters never win; Winners NEVER QUIT!!!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

You'll need a custom comparator that, when comparing, splits the strings based on the space (hint: String.split), then converts the array elements to Integers (or Longs, depending on the length), and then compares these.

As for the comparing of the Integers, you'll need to compare the separate parts. Something like this:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need Help - Sorting an ArrayList of Strings
 
Similar Threads
Sorting
java sql help
Error in calling Enity Bean from Stateless Bean (ejb3.0)
String decoding
sorry... my previous mail got posted halfway...completing now