This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How To Sort Multiple Columns

 
James Gordon
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In above code which is the public class?
 
Liz Brown
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found this link, it has an elegant way to achieve the functionality of Order By Col1, Col2...

http://naukribadlo.com/wordpress/delegate-comparator/
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic