我正在尝试对一个混合了整数和字符串的数组进行排序。例如:
a = ["a", "b", 5, "c", 4, "d", "a1", "a12", 3, 13, 2, "13a", "12a"]
我尝试了:
a.sort do |x, y|
if x.class == y.class
x <=> y
else
x.class.to_s <=> y.class.to_s
end
end
这将返回:
[2, 3, 4, 5, 13, "12a", "13a", "a", "a1", "a12", "b", "c", "d"]
The outcome I want is:
[2, 3, 4, 5, "12a", 13, "13a", "a", "a1", "a12", "b", "c", "d"]
["a1", "a12", "a2"]
还是["a1", "a2", "a12"]
? - Stefan["a1", "a2", "a12"]
- Christian Fazzini