Forums Register Login

Sorting arraylist on the basis of object

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Make your PersonInfo Comparable or make a Comparator<PersonInfo> which will compare personId.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Try reading this.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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..



+Pie Number of slices to send: Send
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..
+Pie Number of slices to send: Send
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read.
+Pie Number of slices to send: Send
I am new to the forum.. had no idea abt code format..

will edit it soon..
Thanks for the suggestion
+Pie Number of slices to send: Send
Also note that in general we prefer not to give direct answers, but rather hints and suggestions: please see DoYourOwnHomework and NotACodeMill.
+Pie Number of slices to send: Send
 

Himanshu Gupta wrote:Try reading this.

Why are you quoting 1.4.2 APIs? They were superseded 5½ years ago.
+Pie Number of slices to send: Send
gud work Mr Kapil
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Welcome to the Ranch kamal preet, after 3½ years!
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 


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)..
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
@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.




  • +Pie Number of slices to send: Send
    @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.

  • +Pie Number of slices to send: Send
    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.)
    +Pie Number of slices to send: Send
     

    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).
    +Pie Number of slices to send: Send
    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.
    what if we put solar panels on top of the semi truck trailer? That could power this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 20797 times.
    Similar Threads
    jstl + forEach + x number of items per row
    Inner SQL
    Various Questions
    Best performance: read a huge file
    A Small Automation Needed
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 29, 2024 05:58:41.