School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,125 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,061 people online right now. Registration is fast and FREE... Join Now!



Using the compareTo method

Using the compareTo method Rate Topic: -----

#1 soltan  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 24
  • Joined: 13-July 08


Dream Kudos: 0

Post icon  Posted 01 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

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



The implementation

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: 01 May 2009 - 01:17 AM

Was This Post Helpful? 0
  • +
  • -


#2 pbl  Icon User is offline

  • Java Lover
  • Icon
  • Group: Mentors
  • Posts: 11,168
  • Joined: 06-March 08


Dream Kudos: 475

Posted 01 May 2009 - 03:48 AM

View Postsoltan, on 1 May, 2009 - 01:15 AM, said:

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

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



The implementation

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
Was This Post Helpful? 1
  • +
  • -

#3 Mikeyp926  Icon User is offline

  • D.I.C Head
  • Icon
  • Group: Contributors
  • Posts: 205
  • Joined: 19-March 09


Dream Kudos: 25

Posted 01 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.
Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is offline

  • Java Lover
  • Icon
  • Group: Mentors
  • Posts: 11,168
  • Joined: 06-March 08


Dream Kudos: 475

Posted 01 May 2009 - 01:27 PM

View PostMikeyp926, on 1 May, 2009 - 11:14 AM, said:

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
Was This Post Helpful? 1
  • +
  • -

#5 soltan  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 24
  • Joined: 13-July 08


Dream Kudos: 0

Posted 01 May 2009 - 03:11 PM

View Postpbl, on 1 May, 2009 - 01:27 PM, said:

View PostMikeyp926, on 1 May, 2009 - 11:14 AM, said:

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 :^:
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

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

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month