sorting - How to guarantee that only `Comparable` elements can be sorted in Java? -
i have implemented sort algorithm in java working on list of integers. want extend support general types. specifically, should able sort list of elements comparable (e.g., implementing comparable
interface).
in haskell, can declare them instances of ord
, declare sort
function ord => [a] -> [a]
, let compiler guarantee elements having ordering can sorted.
my question is:
how guarantee elements having ordering (e.g., implementing comparable interface) can sorted in java?
define sort method using constrained type parameter:
public static <t extends comparable<? super t>> list<t> sort(list<t> xs) { ... }
Comments
Post a Comment