Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 306,833 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,760 people online right now. Registration is fast and FREE... Join Now!




Using the compareTo method

 

Using the compareTo method

soltan

1 May, 2009 - 01:15 AM
Post #1

New D.I.C Head
*

Joined: 13 Jul, 2008
Posts: 23


My Contributions
Hi, I知 having a problem using the compareTo method. The compareTo method is used to store strings in alphabetical order of last name then first name if last name is the same.

That is the interface

CODE

public interface Comparable<T>
{
    
    int compareTo(T rhs);
}


The implementation

CODE

public int compareTo(Object rhs)
    {

        int compare = theLastName().compareTo(rhs.theLastName());
        return (compare !=0 ? compare : theFirstName().compareTo(rhs.theFirstName()));

    }


So as you can see I知 using objects here and I知 using an arrayList to store them , How do i employ my compareTo method and using it to sort my collection with Collection.sort(theArrayList) in my main class ?

Thanks

This post has been edited by soltan: 1 May, 2009 - 01:17 AM

User is offlineProfile CardPM
+Quote Post


pbl

RE: Using The CompareTo Method

1 May, 2009 - 03:48 AM
Post #2

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,962



Thanked: 1187 times
Dream Kudos: 450
My Contributions
QUOTE(soltan @ 1 May, 2009 - 01:15 AM) *

Hi, I知 having a problem using the compareTo method. The compareTo method is used to store strings in alphabetical order of last name then first name if last name is the same.

That is the interface

CODE

public interface Comparable<T>
{
    
    int compareTo(T rhs);
}


The implementation

CODE

public int compareTo(Object rhs)
    {

        int compare = theLastName().compareTo(rhs.theLastName());
        return (compare !=0 ? compare : theFirstName().compareTo(rhs.theFirstName()));

    }


So as you can see I知 using objects here and I知 using an arrayList to store them , How do i employ my compareTo method and using it to sort my collection with Collection.sort(theArrayList) in my main class ?

Thanks


What is T ?
If T is an Object : class Rhs implements Comparable<Object>
then you cannot do:
rhs.theLastName()); because rhs is an Object and Object do not have a method called "theLastName" so you will have to cast

int compareTo(Object o) {
Rsh rsh = (Rhs) o;
... continue as you did

Now if T is and Rhs: class Rhs implements Comparable<Rhs>
then you'll have to change the signature of your method from

int compareTo(Object rhs)
to
int compareTo(Rhs rhs)

hope this hekps


User is online!Profile CardPM
+Quote Post

Mikeyp926

RE: Using The CompareTo Method

1 May, 2009 - 11:14 AM
Post #3

D.I.C Head
Group Icon

Joined: 19 Mar, 2009
Posts: 203



Thanked: 29 times
Dream Kudos: 25
My Contributions
Also, to actually use this method with Collections.sort, I'm thinking you might just call that sort method like you do normally, and it should automatically use the compareTo method you wrote to sort them....at least I think that's how you would do it, I don't think you need to use a comparator or anything, but I could be mistaken.
User is offlineProfile CardPM
+Quote Post

pbl

RE: Using The CompareTo Method

1 May, 2009 - 01:27 PM
Post #4

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,962



Thanked: 1187 times
Dream Kudos: 450
My Contributions
QUOTE(Mikeyp926 @ 1 May, 2009 - 11:14 AM) *

Also, to actually use this method with Collections.sort, I'm thinking you might just call that sort method like you do normally, and it should automatically use the compareTo method you wrote to sort them....at least I think that's how you would do it, I don't think you need to use a comparator or anything, but I could be mistaken.

Ya this the way it works
If you implement Comparable just call the standard Array.sort() method
User is online!Profile CardPM
+Quote Post

soltan

RE: Using The CompareTo Method

1 May, 2009 - 03:11 PM
Post #5

New D.I.C Head
*

Joined: 13 Jul, 2008
Posts: 23


My Contributions
QUOTE(pbl @ 1 May, 2009 - 01:27 PM) *

QUOTE(Mikeyp926 @ 1 May, 2009 - 11:14 AM) *

Also, to actually use this method with Collections.sort, I'm thinking you might just call that sort method like you do normally, and it should automatically use the compareTo method you wrote to sort them....at least I think that's how you would do it, I don't think you need to use a comparator or anything, but I could be mistaken.

Ya this the way it works
If you implement Comparable just call the standard Array.sort() method


Thanks for help , apart from casting my objects , I only needed to sort it using .sort() method to sort them.

great help as always icon_up.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/20/09 10:58PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month