Ad
  • Custom User Avatar

    This does not always return the correct result. For some input strings, it changes relative positions of the characters that are not in the ordering string.

    def sort_string(str,ordering)
    str.chars.sort_by { |c| ordering.index(c) || ordering.size}
    end

    p sort_string("bananapxcgsdfhjd", "anb").join("")
    p "aaannbpxcgsdfhjd"

    the sort_string method results in "aaannbcxp..." instead of "aaannbpxc...". I am running ruby 2.2.0p0