You should use the comparison function for the element type. For example:
utop # List.sort ['d'; 'b'; 'c'] ~compare:Char.compare;;
- : char list = ['b'; 'c'; 'd']
For polymorphic types, the idiom in Base is to have a compare
function that takes another compare
function:
utop # List.sort [Some 3; None; Some 2] ~compare:(Option.compare Int.compare);;
- : int option list = [None; Some 2; Some 3]
For convenience, you can also use something like ppx_compare
, which can generate the correct expression to build the comparison function based on the type you give:
utop # List.sort [Some 3; None; Some 2] ~compare:[%compare: int option];;
- : int option list = [None; Some 2; Some 3]