Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 25th, 2005, 2:00 PM   #1
säkki
Newbie
 
Join Date: Feb 2005
Posts: 9
Rep Power: 0 säkki is on a distinguished road
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.
säkki is offline   Reply With Quote
Old Nov 25th, 2005, 2:22 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
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());
   }
});
Arevos is offline   Reply With Quote
Old Nov 25th, 2005, 2:30 PM   #3
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 373
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
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 !
xavier is offline   Reply With Quote
Old Nov 25th, 2005, 2:50 PM   #4
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
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());
   }
});
(Assuming cInd is descended from List<Person>)
Arevos is offline   Reply With Quote
Old Nov 25th, 2005, 3:17 PM   #5
säkki
Newbie
 
Join Date: Feb 2005
Posts: 9
Rep Power: 0 säkki is on a distinguished road
Quote:
Originally Posted by Arevos
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());
   }
});
(Assuming cInd is descended from List<Person>)
well it actually isn't descended from List<Person>, if I understand what you mean. cInd contains three kind of "persons" could be like this:
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.
säkki is offline   Reply With Quote
Old Nov 25th, 2005, 4:02 PM   #6
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
Quote:
Originally Posted by säkki
well it actually isn't descended from List<Person>, if I understand what you mean. cInd contains three kind of "persons" could be like this:
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.
Java 1.5 has a new feature called generics, which, amongst other things, allows you you to tie a type to a container

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);
In Java 1.5, you can use generics to tell the JVM which type to expect from a container:
Vector<String> list = new Vector<String>();
list.add("Hello");
list.add("World");
String lastElement = list.get(0);
The class Vector<Person> will take Persons, and descedants of Persons (ie. Workers and Students).
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);
}
Of course, if you're not using Java 1.5, you can safely ignore all this talk about generics.

Quote:
Originally Posted by säkki
Thanks for these tips allready, I'll try to get forward in this but I really am miserable in this.
You seem to be doing okay so far
Arevos is offline   Reply With Quote
Old Nov 27th, 2005, 3:01 PM   #7
säkki
Newbie
 
Join Date: Feb 2005
Posts: 9
Rep Power: 0 säkki is on a distinguished road
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);
}
säkki is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:42 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC