给定以下红宝石数组:
["2XL", "3XL", "4XL", "5XL", "6XL", "L", "M", "S", "XL"]
我如何对其进行排序以使其按此顺序排列?
["S", "M", "L", "XL", "2XL", "3XL", "4XL", "5XL", "6XL"]
请注意,并非总是存在每种尺寸。
为了历史的缘故,这是我最初的实现。
sorted_sizes = []
sorted_sizes << "S" if sizes.include?("S")
sorted_sizes << "M" if sizes.include?("M")
sorted_sizes << "L" if sizes.include?("L")
sorted_sizes << "XL" if sizes.include?("XL")
sorted_sizes << "2XL" if sizes.include?("2XL")
sorted_sizes << "3XL" if sizes.include?("3XL")
sorted_sizes << "4XL" if sizes.include?("4XL")
sorted_sizes << "5XL" if sizes.include?("5XL")
sorted_sizes << "6XL" if sizes.include?("6XL")
sorted_sizes