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 Sort a list Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Sort a list" Watch "Sort a list" New topic
Author

Sort a list

Pete Dawn
Greenhorn

Joined: Sep 28, 2006
Posts: 17
Hi everyone,

I have a list in the following format,
A, B, C, 3
X, Y, Z, 2
J, K, L, 4
P, L, M, 1

Now I need to sort this list based on the 4th index (integer). Can somebody tell me how can I go about it.

I've tried Collections.sort, but it doesnt help me when I have numbers greater than 9, like 10, it puts 1,10... etc.

Thanks.
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3188
Have a look at the Comparable and Comparator interfaces.


Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
Pete Dawn
Greenhorn

Joined: Sep 28, 2006
Posts: 17
Can you give me an example. I need to find a quick fix to this one.

Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Use String.split to get the last digit. Convert it into an integer using Integer.parseInt. Compare on the integer.
I'm sure you can turn this into code.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Sort a list
 
Similar Threads
Mergesort referencing Comparable objects
Sorting of List?
ArrayList selection Sort
Flat File Index (Very Urgent)
sorting an ArrayList Alphabetically