f0/\/2!3
08-01-2008, 14:29
Ho un Vector di oggetti di tipo Tree da me definiti: se volessi ordinarli in ordine crescente prendendo come riferimento un parametro interno ad oggetti di tale tipo cosa dovrei usare?
So che esiste la libreria java.util.Collections che presenta un metodo sort dichiarato di questo tipo:
sort
public static <T> void sort(List<T> list,
Comparator<? super T> c)
Sorts the specified list according to the order induced by the specified comparator.
All elements in the list must be mutually comparable using the specified comparator (that is, c.compare(e1, e2)
must not throw a ClassCastException for any elements e1 and e2 in the list).
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.
The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element
in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed
n log(n) performance. The specified list must be modifiable, but need not be resizable. This implementation
dumps the specified list into an array, sorts the array, and iterates over the list resetting each element
from the corresponding position in the array. This avoids the n2 log(n) performance that would result from
attempting to sort a linked list in place.
Parameters:
list - the list to be sorted.
c - the comparator to determine the order of the list. A null value indicates that the elements' natural ordering should be used.
Throws:
ClassCastException - if the list contains elements that are not mutually comparable using the specified comparator.
UnsupportedOperationException - if the specified list's list-iterator does not support the set operation.
See Also:
Comparator
--------------------------------------------------------------------------------
Il problema è che non riesco ad utilizzarlo: forse il problema è con il fatto che lavoro con un vettore invece che con una lista anche se so se che un vettore implementa una lista...
So che esiste la libreria java.util.Collections che presenta un metodo sort dichiarato di questo tipo:
sort
public static <T> void sort(List<T> list,
Comparator<? super T> c)
Sorts the specified list according to the order induced by the specified comparator.
All elements in the list must be mutually comparable using the specified comparator (that is, c.compare(e1, e2)
must not throw a ClassCastException for any elements e1 and e2 in the list).
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.
The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element
in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed
n log(n) performance. The specified list must be modifiable, but need not be resizable. This implementation
dumps the specified list into an array, sorts the array, and iterates over the list resetting each element
from the corresponding position in the array. This avoids the n2 log(n) performance that would result from
attempting to sort a linked list in place.
Parameters:
list - the list to be sorted.
c - the comparator to determine the order of the list. A null value indicates that the elements' natural ordering should be used.
Throws:
ClassCastException - if the list contains elements that are not mutually comparable using the specified comparator.
UnsupportedOperationException - if the specified list's list-iterator does not support the set operation.
See Also:
Comparator
--------------------------------------------------------------------------------
Il problema è che non riesco ad utilizzarlo: forse il problema è con il fatto che lavoro con un vettore invece che con una lista anche se so se che un vettore implementa una lista...