How to sort a string in Ocaml?

Regard with @nojb’s answer, you might also use to_seq of_seq to convert between them

let sort s = 
  String.to_seq s |> List.of_seq |> List.sort Char.compare |> List.to_seq |> String.of_seq;;


let sort s = 
  let a = s |> String.to_seq |> Array.of_seq in
    Array.sort Char.compare a;
    a |> Array.to_seq |> String.of_seq;;
1 Like