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 Sorting arraylist on the basis of object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sorting arraylist on the basis of object" Watch "Sorting arraylist on the basis of object" New topic
Author

Sorting arraylist on the basis of object

Prashant Langade
Ranch Hand

Joined: Jul 01, 2009
Posts: 71
Hi

I have an arraylist of custom objects say List<PersonInfo>
PersonInfo list have attributes like

personId
PersonName
PersonAddress.

Suppose the list contains records as {(1,"Name1","Address1"),(3,"Name3","Address3"),(2,"Name2","Address2")}

Now i want to sort the arraylist on the basis of personId in descending order.
that means my output list would be {(3,"Name3","Address3"),(2,"Name2","Address2")(1,"Name1","Address1")}

Can anyone tell me how do i do that?

Prashant
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Make your PersonInfo Comparable or make a Comparator<PersonInfo> which will compare personId.


[My Blog]
All roads lead to JavaRanch
Prashant Langade
Ranch Hand

Joined: Jul 01, 2009
Posts: 71
ok. but after making it implement Comparable, i need to implement compareTo method



What do i write in that method? and do i need to use Collections.sort() method after that?


Prashant
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Try reading this.


My Blog SCJP 5 SCWCD 5
Nick Puketza
Greenhorn

Joined: Apr 01, 2010
Posts: 16
Hi Prashant,

In addition, if you can, please check out Chapter 7 of the K&B SCJP 6 Study
Guide. It includes some examples that are highly similar to the situation you
describe, and shows how to implement the compareTo() method in detail.

Also, you are right about using the Collections.sort() method.

- Nick
Abhineet Kapil
Ranch Hand

Joined: Feb 08, 2010
Posts: 52

It can be achieved in two ways..

1. implementing Comparable interface
2. implementing Comparator interface

1. DVDInfo Class is the one which has to be sorted say by genre.So it implements Comparable




The Class in which sorting is done..



Abhineet Kapil
Ranch Hand

Joined: Feb 08, 2010
Posts: 52

There is a limitation with Comparable interface . ie out of 4 properties of DVDInfo Class you can sort it by one property only...

This limitation can be overcome by implementing the Comparator Interface..







Similarly you can write a NameComparator Class.. or for any other field...


Hope I have been of help..
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read.
Abhineet Kapil
Ranch Hand

Joined: Feb 08, 2010
Posts: 52

I am new to the forum.. had no idea abt code format..

will edit it soon..
Thanks for the suggestion
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Also note that in general we prefer not to give direct answers, but rather hints and suggestions: please see DoYourOwnHomework and NotACodeMill.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
Himanshu Gupta wrote:Try reading this.
Why are you quoting 1.4.2 APIs? They were superseded 5½ years ago.
kamal preet
Greenhorn

Joined: Sep 11, 2006
Posts: 3
gud work Mr Kapil
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Abhineet Kapil wrote:There is a limitation with Comparable interface . ie out of 4 properties of DVDInfo Class you can sort it by one property only...


Really? Nothing in the compareTo() method javadoc says anything about only being able to compare on one property. It seems to me that would be a matter of how you implement compareTo().

John.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38902
    
  23
Welcome to the Ranch kamal preet, after 3½ years!
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Really? Nothing in the compareTo() method javadoc says anything about only being able to compare on one property. It seems to me that would be a matter of how you implement compareTo().

You're right, there's no such limitation.
Abhineet Kapil
Ranch Hand

Joined: Feb 08, 2010
Posts: 52


There is a limitation with Comparable interface . ie out of 4 properties of DVDInfo Class you can sort it by one property only...


The point is that you can override int compareTo(Object o) method only once in a class that implements Comparable interface...
So you can do it for any one of the property...


Or Please provide an example where we can sort an array with different properties (say empID and empAge)..
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14157
    
  19

Abhineet Kapil wrote:The point is that you can override int compareTo(Object o) method only once in a class that implements Comparable interface...
So you can do it for any one of the property...

It doesn't matter that the compareTo method is called only once.

To sort on multiple properties, do something like this in your compareTo method:

1. Compare the primary properties you want to sort on.
2. If they are not equal, then return the appropriate value (-1 or 1).
3. If they are equal, then compare the secondary properties you want to sort on.
4. If they are not equal, then return the appropriate value (-1 or 1).
5. If they are equal, then compare the ternary properties you want to sort on.
...
6. If all properties are equal, return 0.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Abhineet Kapil
Ranch Hand

Joined: Feb 08, 2010
Posts: 52

It doesn't matter that the compareTo method is called only once.

To sort on multiple properties, do something like this in your compareTo method:

1. Compare the primary properties you want to sort on.
2. If they are not equal, then return the appropriate value (-1 or 1).
3. If they are equal, then compare the secondary properties you want to sort on.
4. If they are not equal, then return the appropriate value (-1 or 1).
5. If they are equal, then compare the ternary properties you want to sort on.
...
6. If all properties are equal, return 0.


Jesper..
You are going a little off the track..

Consider following table..


Now lets sort it by EMP NAme..

(Here we are putting secondary check on EMP AGE so Santosh comes before Sahil)
Eventhough we are putting a secondary check on age , that doesnt mean we are sorting empArray by age...
As you can clearly see age column is still unsorted.. In reality we are still sorting by EmpName..

So for one overrided compareTO() method we can sort by one property only.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That is incorrect: we're talking about sorting by name *and* age. Your sample size is too small to see what this actually means in practice. If two of those rows had the same name (or the same age), they'd be sorted on the second criteria (age or name, depending on what order you're sorting in). If you were sorting by name then age, and two users had the name "Santosh", they'd then be sorted by age. This happens in SQL criteria all the time, in pseudo-SQL:A compareTo method can do the exact same thing.

Sorting on multiple criteria means you sort by one *then* another--one takes precedence.
Abhineet Kapil
Ranch Hand

Joined: Feb 08, 2010
Posts: 52

@David, Young

I take your point. But what I am talking about is a completely different thing.

One thing has to be made very clear that a collection can be sorted by only one property at a time.

Let me take an example to make it clear.

Requirement :
Display two different results.
1. empArray sorted by empID.
2. empArray sorted by empName.

Assumption :
Employee bean class implements Comparable interface and overrides compareTO() for comparing empID..

My Argument :
  • compareTO() method can be overridden only once.
  • It compares empID (If empID is equal then compares by empName).
  • Following above procedure will display one set of data that will be sorted by empID.




  • Abhineet Kapil
    Ranch Hand

    Joined: Feb 08, 2010
    Posts: 52

    @David, Young

    I take your point. But what I am talking about is a completely different thing.

    One thing has to be made very clear that a collection can be sorted by only one property at a time.

    Let me take an example to make it clear.

    Requirement :
    Display two different results.
    1. empArray sorted by empID.
    2. empArray sorted by empName.

    Assumption :
    Employee bean class implements Comparable interface and overrides compareTO() for comparing empID..

    My Argument :
  • compareTO() method can be overridden only once.
    It compares empID (If empID is equal then compares by empName).
    Following above procedure will display one set of data that will be sorted by empID.

  • David Newton
    Author
    Rancher

    Joined: Sep 29, 2008
    Posts: 12617

    Then you probably meant to say "you can only implement a single sort order using Comparable". (Which is also true only if your compareTo doesn't do any logic to decide how to sort.)
    Abhineet Kapil
    Ranch Hand

    Joined: Feb 08, 2010
    Posts: 52

    David Newton
    Today 15:23:40 Subject: Sorting arraylist on the basis of object
    Then you probably meant to say "you can only implement a single sort order using Comparable". (Which is also true only if your compareTo doesn't do any logic to decide how to sort.)


    If compareTo() doesnt provide any sorting logic for a custom object (other than primitives),you will get a runtime error when you try Collections.sort(customObject).
    David Newton
    Author
    Rancher

    Joined: Sep 29, 2008
    Posts: 12617

    I'm not sure what that has to do with what I said, but okay. I meant that compareTo could decide on what property, or properties, to sort, based, say, on other instance variables.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Sorting arraylist on the basis of object