• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How To Sort Multiple Columns

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 62
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic