Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-22-2008, 08:07 AM
Member
 
Join Date: May 2008
Posts: 27
nanaji is on a distinguished road
Send a message via MSN to nanaji Send a message via Yahoo to nanaji
how compareTo Method works
Hi Guys

I have understood until how to reach upto "compreTo" method. But i didn't understand how this "compareTo" method sort the array. Can please some one explain on this. Mainly my confusion is , if you see below code.


Collections.sort(names);
i know the above statement points to below method.

public int compareTo(Name n) {
int lastCmp = lastName.compareTo(n.lastName);
return (lastCmp != 0 ? lastCmp :
firstName.compareTo(n.firstName));
}


My Question is

compreTo(Name n) getting Name Object so that we got n.lastName value.
How it is sorting? a bit confusion.






the above method from this class


import java.util.*;

public class Name implements Comparable<Name> {
private final String firstName, lastName;

public Name(String firstName, String lastName) {
if (firstName == null || lastName == null)
throw new NullPointerException();
this.firstName = firstName;
this.lastName = lastName;
}

public String firstName() { return firstName; }
public String lastName() { return lastName; }

public boolean equals(Object o) {
if (!(o instanceof Name))
return false;
Name n = (Name)o;
return n.firstName.equals(firstName) &&
n.lastName.equals(lastName);
}

public int hashCode() {
return 31*firstName.hashCode() + lastName.hashCode();
}

public String toString() {
return firstName + " " + lastName;
}

public int compareTo(Name n) {
int lastCmp = lastName.compareTo(n.lastName);
return (lastCmp != 0 ? lastCmp :
firstName.compareTo(n.firstName));
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-22-2008, 09:40 PM
Nicholas Jordan's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Southwest
Posts: 880
Nicholas Jordan is on a distinguished road
Further problem statement needed.
Code:
// How it is sorting? a bit confusion. // You are telling collections how to // do the sorting and searching. // CompareTo(Name n) uses String Object // So to compare by n.lastName value: CompareToMethodWorks{ public Name(String firstName, String lastName) { if (firstName == null || lastName == null) throw new NullPointerException(); this.firstName = firstName; this.lastName = lastName; } public int compareTo(Name n) { if( n != null ) && n instanceof String) { String s = (String) n; int result = n.compareTo(s); // ..... deletions awaiting further problem definition. } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
compareTo() Tsiliadoros Advanced Java 5 10-03-2008 03:18 PM
Second level caching in Hibernate: when it works? leonus Database 0 06-04-2008 03:24 PM
Project works in debugger, not after compiling Fleur New To Java 11 05-29-2008 11:04 AM
How DataSource Concept works in Spring Framework Java Tip Java Tips 0 04-01-2008 12:40 PM
How DataSource Concept works in Spring Framework JavaBean Java Tips 0 09-28-2007 02:58 PM


All times are GMT +3. The time now is 11:49 AM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org