• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sorting on multiple columns  RSS feed

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

My requirement is, i need to sort a list which consists of java bean on 3 fields like channel,month,day.

First i need to sort by channel then month and then day.
Can you please share your thoughts to achieve this.

thanks in advance.

tvnrrk.
 
Ranch Hand
Posts: 79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually to implement custom sorting you should write your own Comparator, i.e. class, that implements java.util.Comparator interface.
 
ravi kanth
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranchers,

could you please help me to provide a solution.
My output should be like this.

The values represents fields in a bean.
one row represents one bean object and all the beans are added to list object.
Channnel Month Day
XYZ MAR 12
XYZ FEB 1
XYZ JAN 19
YYY MAY 26
YYY APR 8
YYY JAN 31

i wrote my own comparator class. I am able to sort by channel but after sorting the months are coming in a different format.

could please help me.

thanks,
tvnrrk.
 
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i wrote my own comparator class. I am able to sort by channel but after sorting the months are coming in a different format.

We cannot possible help you until you show us your comparator code
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The order of comparison of the object members might be giving you a wrong ordering.
Please check it.



-Thiyaneshwaran S
 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't compare on the month String values but on their numerical value, e.g. MAR is either 3 (natural value) or Calendar.MARCH (Java value).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!