![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Posts: 9
Rep Power: 0
![]() |
about sort()-method
Here's the problem little simplified:
I have made two classes Person and CardIndex. I created Objects from both Person myP = new Person("FirstName", "LastName"); CardIndex cInd = new CardIndex(); CardIndex is basically a vector where I dump those Persons. Classes have some other stuff in them too, and Person is a super-class for two other Classes, so in CardIndex-vector can be at least three kind of classes or something like that. Problem: I have to sort the cInd by LastName using sort()-method. I'm lost //Collections.sort(cInd**!*"*#) brainMalfunction. Thanks for any help. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
You need a custom Comparator to tell Java how to sort. Comparator objects have a "compare" method that takes two Objects as arguments and returns an integer. If the integer is negative, the first object is less than the second; if the integer is positive, the first object is greater than the second; if the integer is 0, the objects are equal. The String.compareTo method can compare two strings in this way.
You can use an anonymous class to save cluttering up your code with a class that will only be used once. Maybe something like: Collections.sort(cInd, new Comparator() {
int compare(Person a, Person b) {
return a.getLastName().compareTo(b.getLastName());
}
}); |
|
|
|
|
|
#3 |
|
Professional Programmer
|
Shouldn't that be :
Collections.sort(cInd, new Comparator() {
int compare(Object o1, Object o2) {
Person a = (Person)o1;
Person b = (Person)o2;
return a.getLastName().compareTo(b.getLastName());
}
});
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Yes, you're right. Though, if you have Java 1.5:
Collections.sort(cInd, new Comparator<Person>() {
int compare(Person a, Person b) {
return a.getLastName().compareTo(b.getLastName());
}
}); |
|
|
|
|
|
#5 | |
|
Newbie
Join Date: Feb 2005
Posts: 9
Rep Power: 0
![]() |
Quote:
cInd {Person("FN", "LN"), Student("FN", "LN"), Worker("FN", "LN")} <- not meant to be anykind of syntax just to clarify things.Student and Worker are descended from Person. Thanks for these tips allready, I'll try to get forward in this but I really am miserable in this. |
|
|
|
|
|
|
#6 | ||
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Quote:
Before Java 1.5, you had to cast an element as the correct type when removing it from a container: Vector list = new Vector();
list.add("Hello");
list.add("World");
String lastElement = (String)list.get(0);Vector<String> list = new Vector<String>();
list.add("Hello");
list.add("World");
String lastElement = list.get(0);Vector<Person> persons = new Vector<Person>();
persons.add(new Person("A", "B"));
persons.add(new Worker("A", "B"));
persons.add(new Student("A", "B"));
for (Person person : persons) {
System.out.println(person);
}Quote:
![]() |
||
|
|
|
|
|
#7 |
|
Newbie
Join Date: Feb 2005
Posts: 9
Rep Power: 0
![]() |
OK thank you guys, finally got it working. Had some problems with the whole idea (what to do in which class etc). Post my solution here if it may help someone else.
In class Person:
//implements Comparable
-----------------------------
public int compareTo(Object next) {
String bLastN = ((Person)next.getLastName();
int comp = ((Person)this).getLastName().compareTo(bLastN);
if (comp == 0) return 0; //guess this is the only return statement that is
if (comp < 0) return -1; //needed, but the 2 other doesn't seem to do
else return 1; //any harm either
}
------------------------------------------
In class CardIndex:
public void sortItorScrewIt(){
Collections.sort(cInd);
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|