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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -