aspose file tools*
The moose likes Java in General and the fly likes Sorting arrayLIst containing value objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting arrayLIst containing value objects" Watch "Sorting arrayLIst containing value objects" New topic
Author

Sorting arrayLIst containing value objects

kundan varma
Ranch Hand

Joined: Mar 08, 2004
Posts: 322
HI all
I have an arraylist containing value objects. I want to sor this based on three fields of value objects. for eg
name, containerFK and last updated by.
I have overrided compareTo(Object obj) method.For one field its simple
i am just returning "return name.compareTo(obj.name).
What if i want to do this for all the three field.I want to work it like order by function.
THanks
kundan


SCJP1.4,SCBCD,SCEA,CNA
Failures are practice shoots for success.
Vijayendra V Rao
Ranch Hand

Joined: Jul 04, 2004
Posts: 195
Have you tried combining all the three comparisons using boolean operators?


Vijayendra <br /> <br />"The harder you train in peace, the lesser you bleed in war"
kundan varma
Ranch Hand

Joined: Mar 08, 2004
Posts: 322
THat is giving compilation error
Vijayendra V Rao
Ranch Hand

Joined: Jul 04, 2004
Posts: 195
I will give you a rough idea of how you could do this.

Lets say you have two Employee objects and you wish to compare them based on the salary and number of people reporting to each. You could then implement the compareTo( ) method in the following way:



Tis is just a rough idea. Modify the logic according to your requirement.
kundan varma
Ranch Hand

Joined: Mar 08, 2004
Posts: 322
Thanks Vijyandra
This code helped me a lot to think in the right direction
Thanks again
kundan
kundan varma
Ranch Hand

Joined: Mar 08, 2004
Posts: 322
Hi vijay
One more thing.If one of the field is string instead of long then how to use < or > operator.
Thanks
kundan
Vijayendra V Rao
Ranch Hand

Joined: Jul 04, 2004
Posts: 195
Well, this depends on how you want to compare the strings. If you wish to compare them lexicographically, then you would use the compareTo(String) method in the String class. Now suppose you have two strings that you wish to compare:
stringOne and stringTwo

When you use the compareTo(String) method, you would use it as follows:
stringOne.compareTo(stringTwo);

This would return 0 if the two strings are lexicographically equal, a value less than 0 if stringOne is smaller than stringTwo and a value greater than zero if stringOne is greater than stringTwo.

Depends on how you want to compare them. Ofcourse, if its just a normal case and you wish to test two strings for equality, then you would use:

stringOne.equals(stringTwo);

Have your method return 0 is this method returns true, or else return -1.
Vijayendra V Rao
Ranch Hand

Joined: Jul 04, 2004
Posts: 195
Originally posted by Vijayendra V Rao:

stringOne.equals(stringTwo);

Have your method return 0 is this method returns true, or else return -1.


Correction!

Have your method return 0 if this method returns true, or else return -1.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Vijayendra V Rao:
Lets say you have two Employee objects and you wish to compare them based on the salary and number of people reporting to each. You could then implement the compareTo( ) method in the following way:
Sorry, but this comparison is wrong and will lead to no end of confusing bugs and inconsistent behaviour. The Comparable contract states that The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y. The relationship defined by the "comparison" quoted above does not satisfy this: if e1 has salary 10 and numberOfPeopleRep 5, and e2 has salary 5 and numberOfPeopleRep 10, then e1.compareTo(e2) == 1 but e2.compareTo(e1) == 1 as well.

What you need to do is decide the order of significance of the criteria, for example salary is the most important. You then compare in order of significance, comparing the less significant criteria only if the more significant ones are equal:I'd like to highlight a couple of features of this code:
  • It sticks religiously to the contracts for Comparable and Object.equals() and .hashcode(). Do read these contracts. Do verify that your code obeys them to the letter.
  • As suggested in the Comparable javadoc, the equals() method is overridden so that it is compatible with compareTo(), i.e. x.equals(y) and (x.compareTo(y) == 0) always give the same result. This is not an absolute requirement but to do otherwise will be really confusing.
  • The actual value returned by compareTo is not important; the only thing that matters is whether the number is positive, negative or zero. This means that in order to compare two integers, shorts or bytes all you have to do is subtract them; other numeric data types can be compared by returning (diff < 0) ? -1 : (diff > 0) ? +1 : 0.
  • The hashCode() implementation is an exclusive-or of the hashcodes (or values) of the items used in the equals() method. This is generally a decent way to construct hash codes.
  • I'm adding covariant (overloaded) methods for equals() and compareTo(). This is a matter of style - some, like me, find them clear and convenient. They can also shave off a few CPU cycles in some cases because they can save you a cast, but that's not really important.
  • Last but not least, if your methods start to become really complicated then the Commons Lang CompareToBuilder and friends can be really convenient. Another convenient item to know is the Commons Collections ComparatorUtils which can add handling of null values to an existing comparator, chain comparators and things like that.

    - Peter
    [ August 04, 2004: Message edited by: Peter den Haan ]
    Dirk Schreckmann
    Sheriff

    Joined: Dec 10, 2001
    Posts: 7023
    Moving this to the Intermediate forum...


    [How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
    kundan varma
    Ranch Hand

    Joined: Mar 08, 2004
    Posts: 322
    HI peter
    THanks a lot for rectifying me. It was really very good concept. Have you witten any book on these types of concepts.I would like to read that if any.
    THanks again for your help
    kundan
    Peter den Haan
    author
    Ranch Hand

    Joined: Apr 20, 2000
    Posts: 3252
    You might want to read chapter 3 of Josh Bloch's classic Effective Java, published by Addison Wesley. This book comes very highly recommended indeed.

    - Peter
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    Here's how I've done a couple. You could nest the tests if that seems more corect to you ... I like the way they line up if you have more than a couple.

    For primitives replace compoareTo with a ternary ? operator or something.


    A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
    kundan varma
    Ranch Hand

    Joined: Mar 08, 2004
    Posts: 322
    THanks Peter and Stan for your valuable inputs
    Kundan varma
    kundan varma
    Ranch Hand

    Joined: Mar 08, 2004
    Posts: 322
    HI Peter
    I should not ask this question to u but still i thought if you know the answer. Do this book comes in INdian edition.
    If you know the ans plz reply
    Thanks
    kundan
    Peter den Haan
    author
    Ranch Hand

    Joined: Apr 20, 2000
    Posts: 3252
    Not the faintest Kundan, but if it isn't, it should be. It's one of the most highly regarded Java books around.

    - Peter
    kundan varma
    Ranch Hand

    Joined: Mar 08, 2004
    Posts: 322
    Thanks Peter
    Still searching that people are saying adission wesley edition dont come in indian edition.
    Lets see what happens.
    Thanks any way for you help
    kundan
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Sorting arrayLIst containing value objects