Collections.sort(List <T> list) & compareTo(T o)?
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:
Code:
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.
Code:
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);
Re: Collections.sort(List <T> list) & compareTo(T o)?
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.
Re: Collections.sort(List <T> list) & compareTo(T o)?
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?
Code:
public int compareTo(Alpha a){
int i = letter.compareTo(a.letter);
return i;
}
Re: Collections.sort(List <T> list) & compareTo(T o)?
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.
Re: Collections.sort(List <T> list) & compareTo(T o)?
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?
Re: Collections.sort(List <T> list) & compareTo(T o)?
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.
Re: Collections.sort(List <T> list) & compareTo(T o)?
Quote:
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?
Re: Collections.sort(List <T> list) & compareTo(T o)?
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.
Re: Collections.sort(List <T> list) & compareTo(T o)?
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
Re: Collections.sort(List <T> list) & compareTo(T o)?
German's my native language.
Re: Collections.sort(List <T> list) & compareTo(T o)?
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.
ØØ
Re: Collections.sort(List <T> list) & compareTo(T o)?
Wunderbar dessen immer nett nach haben ein kamered deutscher herum. :thumb: Danke schon! CB.
Re: Collections.sort(List <T> list) & compareTo(T o)?
Quote:
Originally Posted by Dilenger4
Wunderbar dessen immer nett nach haben ein kamered deutscher herum.
That means absolutely nothing ;)
Oh, and I'm Austrian.