Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

First Group by one column and then by sorting by another column

 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I have one bean array, which has 3 properties. let us suppose from DB we got 10 records and which will be stored in the bean class. so basically it will become bean array. Now what I need is while displaying First Group by one column and then Sort by another column. Output should be in below format

[can any one please give suggestion on this?

Thanks
Kodavatikanti
 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
heyyy any one please help me......................
 
Rob Camick
Ranch Hand
Posts: 2616
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sort by the secondary column first and then sort by the primary column.
 
Balaji Bang
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob...
But what i need is different. First this must be sorted based on Status column. then if we have same status (like "damaged" 2 times in the table shown) ,then those 2 rows must be sorted based on 2nd column........ This is what I want ..


Please help me.............
 
Rob Camick
Ranch Hand
Posts: 2616
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I gave you the answer. Did you try it?
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's slide this discussion over to a non-certification forum - Java in General...
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob's solution will work but has the disadvantage that you always sort by the second column. This could be a problem with larger datasets. I prefer to use a simple algorithm that only compares the additional columns when it needs to.

I would probably create a Comparator class. In its compare method I would implement code such as the following:

if obj1.columnA equals obj2.columnA
//first column equal so return comparison of second column
return obj1.columnB compareTo obj2.columnB
else
return obj1.columnA compareTo obj2.columnB

Then I would call either Arrays.sort or Collections.sort on your list making sure to use the Comparator you created.

See the Collections Framework for how to implement the sort if you are not familiar with the sorting functionality of Arrays/Collections. Also have a look at the Arrays and Collections utility classes in the java API docs.

Try to understand how the sort mentioned above will work first but if sorting by multiple columns then I tend to use the CompareToBuilder of the Apache Commons Lang package as it makes the code much simpler to understand.
 
Rob Camick
Ranch Hand
Posts: 2616
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let us suppose from DB we got 10 records


Thats why I kept it simple.

I prefer to use a simple algorithm that only compares the additional columns when it needs to


Like the GroupComparator.
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob, I consider:
let us suppose from DB we got 10 records

to be different from "I will only ever have 10 records" hence my suggestion for a different solution for larger datasets.

I had a read through the GroupComparator post and it should work fine but has the disadvantage that you must define a Comparator for each field you want to group by - maybe not ideal if you just want to use the natural ordering of Strings/Integers etc.
Thats where the CompareToBuilder comes in, it has multiple append methods where the natural ordering of objects (Comparable) can be used or alternatively you can specify a Comparator. So the Comparator for "status" then "number" would be as simple as:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic