• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Sort Arraylist by passing runtime parameter

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have Employee class which have age, salary, designation field.
I am adding 15 different element of Employee in Arraylist.
Sometime i want to sort collection on the basis of age, sometime designation or salary.
I tried with comparator, but for one field.
But how can i change sort parameter?

Thanks
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shahid. Welcome to The Ranch!

I'd create a Comparator for every type of sorting you want to do. Then just pass the appropriate one into the sort method. It sounds like what you're doing now - just pass in a different Comparator.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Create 3 Comparators and decide which one to use at runtime.
 
Shahid Pathan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks... I understood
 
Greenhorn
Posts: 6
MyEclipse IDE Hibernate Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shahid Pathan wrote:Thanks... I understood



This will work surely if you want to sort by ageor salary or designation make three comparators as shown below


/*Take a person class
Attributes: name, age, salary

A

1. Add few sample person objects to an array list

2. Sort arraylist based on person’s age in descending order

B

1. Sort arraylist based on person’s salary in ascending order

*/


 
Shahid Pathan
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks...
That is very good example to understand
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
However, make sure you implement the generic versions of Comparator (and List, etc), instead of the non-generic ones in that example. For example:

That way you get a lot more type safety.

The other thing I'll often do is make the comparators nested classes of the class they're comparing, but that's not essential.
 
kamal khanna
Greenhorn
Posts: 6
MyEclipse IDE Hibernate Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
always been a pleasure
Posting java queries.....
 
Ranch Hand
Posts: 54
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
.....I always just used the standard compare method, but used a static variable that defaulted to "x".....but could be set to any other sort that I wanted to make based on whatever I set the variable to...

sloppy sudo code here



Please keep in mind that this was just a quick down and dirty example, but allows you to do the default sort option only, but just set a variable to determine the sort. I usually setup an enum for the sorting variable.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope you never use that in a multi-threaded environment!
 
Jared Malcolm
Ranch Hand
Posts: 54
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:I hope you never use that in a multi-threaded environment!


nah just desktop app....since we've yet to see any code from the original poster one can only assume.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I must admit, I really don't like that approach. Apart from the problem I just mentioned (where you could actually have your sort method change in the middle of the sort algorithm if you're not careful), it's not immediately obvious which method you're using just by looking at the line doing the sorting. You've got to work out what it was last set to. Yes, that may be in the previous line, but there's a lot of potential for reuse.

It also breaks the general convention that Comparable implements the natural ordering.

In a serious application, I tend to use this approach:

Then you can just use it like this:

It might take a little more setting up, but it makes it extremely clear at the point of use.


 
Jared Malcolm
Ranch Hand
Posts: 54
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like that. Truth be told I used that sorting in my C# class in college simply to get done with the silly test...it worked I passed didn't use it again. Like I said there was no possibility in the application it was used in. I do like the implementation that you provided though. Doesn't look like it will take that much extra time if any then setting up and enum + the if/else/switch/whatever to determine how to do the sorting.
 
I brought this back from the farm where they grow the tiny ads:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic