let idxsort (cmp : 'a -> 'a -> int) (a : 'a array) : int array = let a = Array.mapi a ~f:(fun i b -> (i, b)) in Array.sort ~cmp:(fun a b -> cmp (snd a) (snd b)) a; Array.map ~f:fst a