aspose file tools*
The moose likes Java in General and the fly likes Sorting through Comparable(Very Urgent) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting through Comparable(Very Urgent)" Watch "Sorting through Comparable(Very Urgent)" New topic
Author

Sorting through Comparable(Very Urgent)

Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
Hi I have two classes name Name.java and NameSort.java
Code of Name.java:

Code of NameSort.java:

In above code I can do sort either by firstName or LastName. I also want to do by number(String format only). I can't. Why???
Thanks,
Angela
(Edited by Cindy to format code. You know Angela - you have been around here enough to know that you should format the code yourself using the [ code] and [ /code] tags, without the spaces of course.)
[ August 13, 2002: Message edited by: Cindy Glass ]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Can you tell us more about what happens when you try?
One problem I see in this code is that equals() and hashCode() entirely ignore the "number" field. You'll want to change that if you ever want to put Names into into a HashSet or HashMap.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
In your NameSort class you have your input parameters to Name strangly arranged.
n[0] has a first name of 10, last name of Alvin and number of Lennon. I doubt that that is what you intended.
Next - in your compareTo method - I guess that you are going to PRAY that the only objects coming to you are truely Name objects - because you do not code for any other situation.
Next - - in your compareTo method - you are comparing to lastname (which is Alvin ) and if those are equal then you compare to first name (which is 10) - but nowhere do you mention number at all.
[ August 13, 2002: Message edited by: Cindy Glass ]

"JavaRanch, where the deer and the Certified play" - David O'Meara
Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
I am learning this sorting of object. I just grab this code from Internet. I am trying to change it so I can sort anything I want. But I don't have clue how to start.So if someone has good example or suggestion please help me out.
Thanks again,
Angela
Neil Laurance
Ranch Hand

Joined: Jul 18, 2002
Posts: 183
Why not create instances of Comparator for each type of sort you would like. Example code would be something like:

Then you can call the required sort mechanism using:

One point to make: do you really want 'number' to be a String? This would mean we perform a lexical sort as opposed to a numerical sort for this attribute.
[changed code from Comparable to Comparator]
Hope this makes sense. Cheers, Neil
[ August 13, 2002: Message edited by: Neil Laurance ]
[ August 13, 2002: Message edited by: Neil Laurance ]
[ August 13, 2002: Message edited by: Neil Laurance ]
Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
Thanks Neil,
The following code does sort by length of string instead ascending order.Eg. I put August 13, August 9, August 2. It will sort like August 13, August 2, August 9 instead August 2, August 9, August 13.
Would you please help me out? If you need to view code:
Code for NameSort.java:
import java.util.*;
class NameSort {
public static void main(String args[]) {
Name [] n= new Name[4];
n[0] = new Name("10", "Rakeh", "Pandit");
n[1] = new Name("20","Rakesh", "Pandit");
n[2] = new Name("90","Angela", "Desouza");
n[3] = new Name("100", "Ed", "Lin");
List set = new ArrayList();
set.add(n[0]);
set.add(n[1]);
set.add(n[2]);
set.add(n[3]);
Collections.sort(set,Name.FIRST);
ListIterator it = set.listIterator();
while(it.hasNext()){
Name name = (Name)it.next();
System.out.println(name.lastName + " " + name.firstName);
}
}
}
Code for Name.java:
import java.util.*;
class Name
{
public String number, firstName, lastName;
public Name(String number,String firstName, String lastName)
{
if (firstName==null || lastName==null)
throw new NullPointerException();
this.firstName = firstName;
this.lastName = lastName;
this.number = number;
}
public static final Comparator FIRST = new Comparator()
{
public int compare(Object o1, Object o2)
{
try
{
Name name1 = (Name)o1;
Name name2 = (Name)o2;
return name1.firstName.compareTo(name2.firstName);
}
catch(ClassCastException e)
{
// do something here
}
return 0;
}
// do something for equals too
};
public static final Comparator LAST = new Comparator()
{
public int compare(Object o1, Object o2)
{
try
{
Name name1 = (Name)o1;
Name name2 = (Name)o2;
return name1.lastName.compareTo(name2.lastName);
}
catch(ClassCastException e)
{
// do something here
}
return 0;
}
// do something for equals too
};
public static final Comparator NUMBER = new Comparator()
{
public int compare(Object o1, Object o2)
{
try
{
Name name1 = (Name)o1;
Name name2 = (Name)o2;
return name1.number.compareTo(name2.number);
}
catch(ClassCastException e)
{
// do something here
}
return 0;
}
// do something for equals too
};
}
Please help me out.
Thanks,
Angela
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
You're storing and sorting the number field as a string, so "August 13" does indeed sort before "August 2".
If it's a number, store it as an int, and compare it that way.
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

the sort will work if you use MMDD format instead of the longer values. So August 13 becomes "0813" and August 8 becomes "0808"...etc. This will also work if you use August 08 instead of August 8.

Jamie
[ August 14, 2002: Message edited by: Jamie Robertson ]
Angela D'souza
Ranch Hand

Joined: Jan 16, 2002
Posts: 469
Originally posted by Jamie Robertson:
the sort will work if you use MMDD format instead of the longer values. So August 13 becomes "0813" and August 8 becomes "0808"...etc. This will also work if you use August 08 instead of August 8.
[ August 14, 2002: Message edited by: Jamie Robertson ]

How can I get MMDD format???
Thanks,
Angela
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The second article in my series on Collections has an example of setting up Comparators. Check the JavaRanch newsletter. I think it's in the July issue.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Neil Laurance
Ranch Hand

Joined: Jul 18, 2002
Posts: 183
If you have one of your strings that always contains dates, I would convert these into a private java.util.Date attribute within the Name class. Then do your comparison using these Date values.
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

If I were you, I would parse the complete date string ( including the year ) using a SimpleDateFormat ( using the pattern your date string is in ) and compare the Date values rather than the String values of the date as mentioned earlier. As an alternative, you could also compare the millisecond representation of the Date or as suggested by me earlier, format the date into the desired "MMdd" String pattern and compare those.
just for my information, why isn't there a year value associated with every date?
Jamie
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting through Comparable(Very Urgent)
 
Similar Threads
help on Comparable
Pesudo coding
TreeSet problem
TreeSet problem: not able to add two different instances to same set.