This week's book giveaway is in the Spring forum.
We're giving away four copies of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 and have Marten Deinum & Iuliana Cosmina on-line!
See this thread for details.
Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Sorting arraylist on the basis of object

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make your PersonInfo Comparable or make a Comparator<PersonInfo> which will compare personId.
 
Prashant Langade
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try reading this.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 52
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read.
 
Abhineet Kapil
Ranch Hand
Posts: 52
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am new to the forum.. had no idea abt code format..

will edit it soon..
Thanks for the suggestion
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also note that in general we prefer not to give direct answers, but rather hints and suggestions: please see DoYourOwnHomework and NotACodeMill.
 
Marshal
Posts: 72938
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Himanshu Gupta wrote:Try reading this.

Why are you quoting 1.4.2 APIs? They were superseded 5½ years ago.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
gud work Mr Kapil
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 72938
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch kamal preet, after 3½ years!
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 52
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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)..
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Abhineet Kapil
Ranch Hand
Posts: 52
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 52
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
    Posts: 52
    Eclipse IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    @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
    Posts: 12617
    IntelliJ IDE Ruby
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 52
    Eclipse IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
    Posts: 12617
    IntelliJ IDE Ruby
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    No matter how many women are assigned to the project, a pregnancy takes nine months. Much longer than this tiny ad:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic