File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Sorting question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting question" Watch "Sorting question" New topic

Sorting question

Lenin Chada

Joined: Mar 10, 2003
Posts: 3
can any body please give me an sample code to sort on 2nd, 3rd, or 4th item
Array.sort(A) is sorting on first item
I written this comparator code
class StringCompare2 implements Comparator {
public int compare(Object o1, Object o2) {
String s1 = (String)o1;
String s2 = (String)o2;
return s1.compareTo(s2);

A[1] = [xx, aa, yy, 22, zz]
A[2] = [aa, xx, bb, 33, cc]
A[3] = [dd, yy, cc, 11, xx]
I want sort on 3 rd item in that array
Result like
A[1] = [aa, xx, bb, 33, cc]
A[2] = [dd, yy, cc, 11, xx]
A[3] = [xx, aa, yy, 22, zz]
thanks in advance
Typo in title corrected -BFG
[ March 10, 2003: Message edited by: Barry Gaunt ]
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

It seems like you need to send the third element of the array to be compared, and that would be A[1][3] and A[2][3] and A[3][3], ...

Please ignore post, I have no idea what I am talking about.
I agree. Here's the link:
subject: Sorting question
It's not a secret anymore!