Comparable Interface - Sort by multiple charateristics

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1533 Views - Last Post: 06 March 2011 - 09:27 AM Rate Topic: -----

#1 rigohernandez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-February 11

Comparable Interface - Sort by multiple charateristics

Posted 05 March 2011 - 11:09 AM

Hey guys,

I'm trying to get my head wrapped around the Comparable interface. I'm trying to sort by a number of things, 1.Class, 2.Company 3.EmployeeNo. I know how to sort a single characteristic, but I'm not sure how to sort 2 or more fields. The following won't work, because the obvious "if" statement is missing so it never gets to the second sorting criteria.

My thought is that I need to use if statements to see if we're company the class, company or the employee. Is that correct? If so, how would I check that? I'm also a little confused because we're comparing Strings instead of numbers (int).


    public int compareTo(Object other)
    {
        Person that = (Person) other;

        String class1 = this.getClass().getName();
        String class2 = that.getClass().getName();
        int result = class1.compareTo(class2);
        //if(result != 0) return result;   WILL THIS WORK?
        
        String comp1 = this.company;
        String comp2 = that.company;
        result = comp1.compareTo(comp2);
        //if(result != 0) return result;   WILL THIS WORK?

        String num1 = this.employeeNum;
        String num2 = that.employeeNum;
        result = num1.compareTo(num2);
        //if(result != 0) return result;   WILL THIS WORK?

        return 0;
    }




Thanks for the help!

Is This A Good Question/Topic? 0
  • +

Replies To: Comparable Interface - Sort by multiple charateristics

#2 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Comparable Interface - Sort by multiple charateristics

Posted 05 March 2011 - 11:11 AM

Yes those ifs "will work" as long as the class names alphabetical sort order is what you want.
Was This Post Helpful? 1
  • +
  • -

#3 rigohernandez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-February 11

Re: Comparable Interface - Sort by multiple charateristics

Posted 05 March 2011 - 11:18 AM

View Postmasijade, on 05 March 2011 - 11:11 AM, said:

Yes those ifs "will work" as long as the class names alphabetical sort order is what you want.

Talk about a FAST response!!!
Masijade, your awesome!! Thanks for the reply.

Does the code look short/efficient or would you suggest doing it some other way??

Thanks again for the reply! :bigsmile:

This post has been edited by rigohernandez: 05 March 2011 - 11:19 AM

Was This Post Helpful? 0
  • +
  • -

#4 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Comparable Interface - Sort by multiple charateristics

Posted 05 March 2011 - 11:22 AM

Short enough, but you should probably first check whether the argument is null and then also check whether it is even castable to Person (before doing so or by catching the ClassCastException) and then return (in your mind) a valid value for those cases so that your method does not throw a RuntimeException.
Was This Post Helpful? 1
  • +
  • -

#5 rigohernandez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-February 11

Re: Comparable Interface - Sort by multiple charateristics

Posted 05 March 2011 - 11:43 AM

View Postmasijade, on 05 March 2011 - 11:22 AM, said:

Short enough, but you should probably first check whether the argument is null and then also check whether it is even castable to Person (before doing so or by catching the ClassCastException) and then return (in your mind) a valid value for those cases so that your method does not throw a RuntimeException.


How would I check if its castable? Never even thought doing that. Forgive me for my ignorance. I'm fairly new to Java/programming.

I'll have to do some reading on ClassCastExceptions. Thanks for the suggestion.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9038
  • View blog
  • Posts: 33,525
  • Joined: 27-December 08

Re: Comparable Interface - Sort by multiple charateristics

Posted 05 March 2011 - 11:56 AM

Comparable is generic. So your param should be a Person, not an Object. Also, make sure you are implementing Comparable as such: Comparable<Person>.
Was This Post Helpful? 2
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 8,802
  • Joined: 20-September 08

Re: Comparable Interface - Sort by multiple charateristics

Posted 05 March 2011 - 12:30 PM

String class1 = this.getClass().getName();
String class2 = that.getClass().getName();
int result = class1.compareTo(class2);
//if(result != 0) return result;   WILL THIS WORK?


What's the intention of the above? 'class1' will ALWAYS equal 'class2' so this block is effectively redundant
Was This Post Helpful? 0
  • +
  • -

#8 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Comparable Interface - Sort by multiple charateristics

Posted 05 March 2011 - 12:40 PM

View Postg00se, on 05 March 2011 - 08:30 PM, said:

String class1 = this.getClass().getName();
String class2 = that.getClass().getName();
int result = class1.compareTo(class2);
//if(result != 0) return result;   WILL THIS WORK?


What's the intention of the above? 'class1' will ALWAYS equal 'class2' so this block is effectively redundant


Not necessarily. Classes that extend Person?
Was This Post Helpful? 0
  • +
  • -

#9 rigohernandez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-February 11

Re: Comparable Interface - Sort by multiple charateristics

Posted 05 March 2011 - 12:42 PM

View Postg00se, on 05 March 2011 - 12:30 PM, said:

String class1 = this.getClass().getName();
String class2 = that.getClass().getName();
int result = class1.compareTo(class2);
//if(result != 0) return result;   WILL THIS WORK?


What's the intention of the above? 'class1' will ALWAYS equal 'class2' so this block is effectively redundant


Goose - We're asked to sort by class name first, then by company and lastly by ID. That's why I used xxx.getClass().getName();

MacOSX - Yes, I was thinking of changing object to Person and skipping the type casting all together.
Was This Post Helpful? 0
  • +
  • -

#10 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Comparable Interface - Sort by multiple charateristics

Posted 05 March 2011 - 12:43 PM

View Postrigohernandez, on 05 March 2011 - 08:42 PM, said:

MacOSX - Yes, I was thinking of changing object to Person and skipping the type casting all together.


Make sure though that the class declaration includes "implements Comparable<Person>" though.

This post has been edited by masijade: 05 March 2011 - 12:44 PM

Was This Post Helpful? 1
  • +
  • -

#11 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 8,802
  • Joined: 20-September 08

Re: Comparable Interface - Sort by multiple charateristics

Posted 05 March 2011 - 12:48 PM

Quote

Not necessarily. Classes that extend Person?


Yes, but Comparable is not really intended to do that, and i don't think (fortunately) that's the intention here either
Was This Post Helpful? 0
  • +
  • -

#12 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Comparable Interface - Sort by multiple charateristics

Posted 05 March 2011 - 12:51 PM

View Postg00se, on 05 March 2011 - 08:48 PM, said:

Quote

Not necessarily. Classes that extend Person?


Yes, but Comparable is not really intended to do that, and i don't think (fortunately) that's the intention here either


I don't see why it couldn't be used to have a Person class then sort the Employees from the Contractors and the Supervisors, etc, etc, etc.

Although, generally I would say to use separate lists for those it is possible and completely acceptable.

Edit: Especially if it is a list that is being "fed" into the current "process".

This post has been edited by masijade: 05 March 2011 - 12:57 PM

Was This Post Helpful? 0
  • +
  • -

#13 rigohernandez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-February 11

Re: Comparable Interface - Sort by multiple charateristics

Posted 05 March 2011 - 12:59 PM

View Postmasijade, on 05 March 2011 - 12:43 PM, said:

View Postrigohernandez, on 05 March 2011 - 08:42 PM, said:

MacOSX - Yes, I was thinking of changing object to Person and skipping the type casting all together.


Make sure though that the class declaration includes "implements Comparable<Person>" though.


Yeah, I noticed that detail "<Person>" while reading about it. Thanks for pointing it out.
Was This Post Helpful? 0
  • +
  • -

#14 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4888
  • View blog
  • Posts: 11,282
  • Joined: 16-October 07

Re: Comparable Interface - Sort by multiple charateristics

Posted 05 March 2011 - 02:15 PM

If the object is of a different type, choose a default, like 1.

For the same:
class Person implements Comparable<Person> {
	private String name, company, employeeNum;
	
	public int compareTo(Person that) {
		if (this==that) { return 0; }
		if (that==null) { return 1; }
		
		int result = this.name.compareTo(that.name);
		if (result==0) { 
			result = this.company.compareTo(that.company);
			if (result==0) { 
				result = this.employeeNum.compareTo(that.employeeNum);
			}
		}
		return result;
	}
}



If you want to allow for nulls:
public int compareTo(Person that) {
	if (this==that) { return 0; }
	if (that==null) { return 1; }
	
	if (this.name==null && that.name!=null) { return -1; }
	int result = this.name.compareTo(that.name);
	if (result!=0) { return result; }
	if (this.company==null && that.company!=null) { return -1; }
	result = this.company.compareTo(that.company);
	if (result!=0) { return result; }
	if (this.employeeNum==null && that.employeeNum!=null) { return -1; }
	return this.employeeNum.compareTo(that.employeeNum);
}


Was This Post Helpful? 2
  • +
  • -

#15 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 195
  • View blog
  • Posts: 1,619
  • Joined: 13-March 10

Re: Comparable Interface - Sort by multiple charateristics

Posted 05 March 2011 - 02:27 PM

As it was mentioned above, in your compareTo method you should not have Object as the parameter but Person, then no ClassCastException is needed etc.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2