PDA

Click to See Complete Forum and Search --> : Collections.sort(List <T> list) & compareTo(T o)?


Dillinger4
Jun 27th, 2005, 06:44 PM
I am a bit confused on how the sort method of the Collections class uses compareTo. Say for instance there is a compareTo() method which takes Alpha objects as a parameter. Is the letter of the first element being compared to the second elements letter field? Also when the value of i is printed out to the console it's not 0,1 0r -1. :confused:

public int compareTo(Alpha a){
int i = letter.compareTo(a.letter);
return i;
}

The Alpha objects just have a field named letter which is a String type.

List<Alpha> alpha = new ArrayList<Alpha>();
alpha.add(new Alpha("z"));
alpha.add(new Alpha("g"));
alpha.add(new Alpha("k"));
alpha.add(new Alpha("q"));
alpha.add(new Alpha("a"));
alpha.add(new Alpha("b"));
alpha.add(new Alpha("o"));
alpha.add(new Alpha("v"));
alpha.add(new Alpha("c"));

Collections.sort(alpha);
System.out.println(alpha);

CornedBee
Jun 28th, 2005, 03:09 AM
Yes, that code ought to sort the letters according to their order.

compareTo isn't required to return -1, 0 or 1. It's required to return negative, zero or positive.

Dillinger4
Jun 28th, 2005, 06:19 PM
Thanks for the reply CB. Yes i guess the value returned from compareTo is a negative integer, zero, or positive ! -1, 0 or 1. More importantly though how does the middle line in the code snippet work?. The line is very confusing. Take the first two Alpha elements within the list. Say Alpha1 has a field "b" and Alpha2 has a field "a". Now it's obvious that they will be swapped. Now which element does a.letter refer to and which element does letter.compar...... refer to?

public int compareTo(Alpha a){
int i = letter.compareTo(a.letter);
return i;
}

CornedBee
Jun 28th, 2005, 07:43 PM
Errm, it just calls the compareTo method of String. String does a lexicographical comparison of the two strings. So it returns -1 if the object on which the method is called is lexicographically less than the parameter, 0 if it's the same, 1 if it's more. The variable i in the method is completely useless, of course.

Dillinger4
Jun 28th, 2005, 08:02 PM
The thing is that i don't understand the line int i = letter.compareTo(a.letter); Is it this.letter being compared to a.letter(which is the second element's letter field)? then letter is the second elements letter field and a.letter is the thirds?

CornedBee
Jun 29th, 2005, 05:26 AM
The line compares this.letter to a.letter, indeed. I don't quite get what you mean by first, second and third element. The sort algorithm will call the comparison function as needed. It might compare any two elements of the sequence.

Dillinger4
Jun 29th, 2005, 02:23 PM
Posted by CornedBee

I don't quite get what you mean by first, second and third element. The sort algorithm will call the comparison function as needed. It might compare any two elements of the sequence.

But shouldn't as needed be for every element? Wouldn't each element have to be compared to each subsequent element or how else would be able to determine it's equality compared to the others?

CornedBee
Jun 29th, 2005, 04:43 PM
Do you know how various sorting algorithms work? Bubble sort, insertion sort, selection sort, merge sort, heap sort, quick sort? If not, I recommend you buy a good book on the topic. I'm afraid I can't recommend one, as all my literature on the topic only was released in German.

Dillinger4
Jun 29th, 2005, 06:04 PM
Some sorting algorithms i am familar with like bubble sorts. Others im not. Im looking into purchasing "The Art of Computer Programming" by Donald E. Knuth.
which i found on various sites. Off topic. You read German? I read a little but not enough to be able to read a whole book.

http://www.amazon.com/exec/obidos/t...=glance&s=books

http://search.barnesandnoble.com/bo...201485419&itm=1

CornedBee
Jun 30th, 2005, 04:06 AM
German's my native language.

NoteMe
Jun 30th, 2005, 07:27 AM
The book I recommended for you in the ChitChat thread is in English and covers all these algorithms ++. But it isn't too advanced. Like for QuickSort it only shows you the default way of doing it. In the advnaced algorithm course at our Uni we had 3 different ways in our book, but that book doesn't cover any of the simple sorting routines as Bubble sort, insertion sort, selection sort, merge sort, heap sort.



ии

Dillinger4
Jun 30th, 2005, 11:38 AM
Wunderbar dessen immer nett nach haben ein kamered deutscher herum. :thumb: Danke schon! CB.

CornedBee
Jul 1st, 2005, 05:21 AM
Wunderbar dessen immer nett nach haben ein kamered deutscher herum.

That means absolutely nothing ;)
Oh, and I'm Austrian.