aspose file tools*
The moose likes Java in General and the fly likes How To Sort Multiple Columns Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How To Sort Multiple Columns" Watch "How To Sort Multiple Columns" New topic
Author

How To Sort Multiple Columns

James Gordon
Ranch Hand

Joined: Aug 09, 2002
Posts: 106
Hi,
Could anyone please give me some ideas on how to sort multiple columns with Collections? - let's say I have an object with 3 fields in an ArrayList; field1, field2 and field3.
This can be done easily in SQL using ORDER BY field1, field2, field3 but I don't want to make use of any databases.
The sort() method seemed to support only 1 column sorting and I'm not sure how we can use it to achieve multiple columns instead.

Thanks in advance.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Have a look at the java.util.Comparator interface and its use in java.util.Collections.sort(List, Comparator)
This allows you to define an absolute ordering on you objects.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi David
what i understand from James questions is "we want to sort data for three columns". Comparator interface and sort() works for a set of objects thats it and that set of objects is one column for us.
it will not be same as ORDer by field1,field2,field3. do you see what i mean here?
i guess, we have say three Object[] here. e.g.
Object[] field1;
Object[] field2;
Object[] field3;
and we want the same functionality like Order by field1,field2,field3 of database. using Comparator and sort() we can do sort on any of these individually but not on (field1,field2,field3). am i making sense?
let me think about how to do this.
thanks!
maulin
Sayed Ibrahim Hashimi
Ranch Hand

Joined: May 17, 2001
Posts: 148
I don't know anything about Database programming so I'm not sure what it is that you are trying to acomplish but it seems like you are tyring to accomplish something like the following. Lets say you have this list:
LastName FirstName SSN
Adams John 123456789
Brown Mike 456123789
Brown Chris 789456132
Sort by LastName, if LastName is equal then sort by FirstName if FirstName is also equal then sort by SSN. Is this what you are trying to accomplish?
Or is it something different? If it is then this isn't really that complicated and you can use Collections.sort(List,Comparator).


SCJP 1.4<br /><a href="http://www.cise.ufl.edu/~sih" target="_blank" rel="nofollow">www.cise.ufl.edu/~sih</a>
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi david,
i agree we can use Comparator and sort() for achieving this but we need to have compare() method having the appropriate algorithm.
here is the example i tried,

input file "orderby.txt" is as follows,
---------------------
vasavada , Maulin , H
vasavada , Niragi , H
Vasavada , Niragi , H
Mehta , Soham , R
Mehta , Soham , J
---------------------
output is,
---------------------
Data before sorting
vasavada,Maulin,H
vasavada,Niragi,H
Vasavada,Niragi,H
Mehta,Soham,R
Mehta,Soham,J
***************************************
Data after sorting
Mehta,Soham,J
Mehta,Soham,R
Vasavada,Niragi,H
vasavada,Maulin,H
vasavada,Niragi,H

Hope this helps.
maulin
---------------------
James Gordon
Ranch Hand

Joined: Aug 09, 2002
Posts: 106
Hi,
Thanks Maulin.
That's exactly what I'm trying to do.
Your code works pretty well!
I'll spent some time to digest the logic for it.

Thanks to the others as well.
Liz Brown
Ranch Hand

Joined: Oct 22, 2002
Posts: 112
In above code which is the public class?
Liz Brown
Ranch Hand

Joined: Oct 22, 2002
Posts: 112
got to work this program. It's a wonderful code. thanks.
however, please let me know the algorithm for this. Same here- difficult to understand the logic.
joy b chakravarty
Ranch Hand

Joined: May 16, 2011
Posts: 62

Found this link, it has an elegant way to achieve the functionality of Order By Col1, Col2...

http://naukribadlo.com/wordpress/delegate-comparator/


Cheers, Joy [SCJP 1.4, SCBCD 5.0]
get high on alcohol, algorithm or both
 
 
subject: How To Sort Multiple Columns