有没有一种简单的方法在Crystal中对数组进行排序?
答
不知道你的数组实际包含我假设它是一个数组(Int32)(整数数组)。
您可以轻松地进行排序整数像
[4, 7, 2].sort { |x, y| y <=> x } # => [7, 4, 2]
阵列检查水晶核心阵列规格更多信息https://github.com/crystal-lang/crystal/blob/bf6b743aa7649ed3ecc92dd06fde21f88460720a/spec/std/array_spec.cr#L998-L1047
+5
这是一个反向排序,'[4, 7,2] .sort'应该足够了。 – Stefan
答
可以使用sort
像Ruby
puts [7,3,2,8].sort #=> [2, 3, 7, 8]
的[文档](https://crystal-lang.org/api/Array.html#sort-instance-method)通常有帮助;-) – Stefan