4 Replies - 2432 Views - Last Post: 01 May 2009 - 04:11 PM Rate Topic: -----

#1 soltan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 13-July 08

Using the compareTo method

Post icon  Posted 01 May 2009 - 02:15 AM

Hi, Iím 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ím using objects here and Iím 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 - 02:17 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Using the compareTo method

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Using the compareTo method

Posted 01 May 2009 - 04:48 AM

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

Hi, Iím 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ím using objects here and Iím 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
  • member icon

Reputation: 29
  • View blog
  • Posts: 205
  • Joined: 20-March 09

Re: Using the compareTo method

Posted 01 May 2009 - 12:14 PM

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

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Using the compareTo method

Posted 01 May 2009 - 02: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

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 13-July 08

Re: Using the compareTo method

Posted 01 May 2009 - 04: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
  • +
  • -

Page 1 of 1