File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Sort Arraylist by passing runtime parameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sort Arraylist by passing runtime parameter" Watch "Sort Arraylist by passing runtime parameter" New topic
Author

Sort Arraylist by passing runtime parameter

Shahid Pathan
Greenhorn

Joined: Dec 28, 2010
Posts: 23
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
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4375
    
    8

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.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Create 3 Comparators and decide which one to use at runtime.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Shahid Pathan
Greenhorn

Joined: Dec 28, 2010
Posts: 23
Thanks... I understood
kamal khanna
Greenhorn

Joined: May 10, 2011
Posts: 6

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

Joined: Dec 28, 2010
Posts: 23
Thanks...
That is very good example to understand
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4375
    
    8

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

Joined: May 10, 2011
Posts: 6

always been a pleasure
Posting java queries.....
Jared Malcolm
Ranch Hand

Joined: May 02, 2011
Posts: 54

.....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.


SCJA 6 (Studying for SCJP 6)
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4375
    
    8

I hope you never use that in a multi-threaded environment!
Jared Malcolm
Ranch Hand

Joined: May 02, 2011
Posts: 54

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

Joined: Apr 06, 2010
Posts: 4375
    
    8

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

Joined: May 02, 2011
Posts: 54

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.
 
jQuery in Action, 2nd edition
 
subject: Sort Arraylist by passing runtime parameter