Ruby-on-rails 按整数值对字符串数组进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7726968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:08:44  来源:igfitidea点击:

Sort an Array of Strings by their Integer Values

ruby-on-railsrubyruby-on-rails-3

提问by tob88

Let's say I have an unsorted array from 1 to 10, as shown below...

假设我有一个从 1 到 10 的未排序数组,如下所示......

a = ["3", "5", "8", "4", "1", "2", "9", "10", "7", "6"]

If I use the sort method on this array, it returns this...

如果我在这个数组上使用 sort 方法,它会返回这个......

a.sort = ["1", "10", "2", "3", "4", "5", "6", "7", "8", "9"]

As you can see, the 10, appears before the 2, which is incorrect. How can I sort these numbers so that 10 appears correctly?

如您所见,10 出现在 2 之前,这是不正确的。如何对这些数字进行排序,以便 10 正确显示?

EDIT: Hi guys, thank you all for your responses. I should explain my problem a little better. The array I need sorted is for an e-commerce price list. So the array appears as follows...

编辑:大家好,谢谢大家的回复。我应该更好地解释我的问题。我需要排序的数组用于电子商务价格表。所以数组如下所示......

a = ["0-10", "11-20", "21-30", "31-40" etc.]

So the strings cannot be converted to integers. I should have put this when I wrote the question. I did not think that there would be much difference in the fix. My mistake, I apologise for making this assumption! How though can I sort this array? Thanks!

所以字符串不能转换为整数。当我写这个问题时,我应该把这个写出来。我不认为修复会有太大差异。我的错误,我为做出这个假设而道歉!我怎样才能对这个数组进行排序?谢谢!

回答by Wizard of Ogz

I'll throw another method out there since it's the shortest way I can think of

我会抛出另一种方法,因为这是我能想到的最短方法

a.sort_by(&:to_i)

回答by apneadiving

As your updated question states:

正如您更新的问题所述:

array.sort_by {|elt| ary = elt.split("-").map(&:to_i); ary[0] + ary[1]}

even geekier:

更极客:

array.sort_by {|elt| ary = elt.split("-").map(&:to_i).inject(&:+)}

回答by Matt

If you convert all strings to integers beforehand, it should work as expected:

如果事先将所有字符串转换为整数,它应该按预期工作:

a.map(&:to_i).sort
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

回答by bricker

a.sort { |a,b| a.to_i <=> b.to_i }

回答by Jéssica Antunes

Another option, that can be used in your example or others arrays like

另一个选项,可用于您的示例或其他数组,如

a = ["teste", "test", "teste2", "tes3te", "10teste"]

is:

是:

a.sort_by! {|s| s[/\d+/].to_i}

回答by Paul Sasik

The reason for this behavior is that you have an array of strings and the sort that is being applied is string-based. To get the proper, numeric, sorting you have to convert the strings to numbers or just keep them as numbers in the first place. Is there a reason that your array is being populate with strings like this:

这种行为的原因是您有一个字符串数组,并且正在应用的排序是基于字符串的。要获得正确的数字排序,您必须将字符串转换为数字,或者首先将它们保留为数字。您的数组是否有这样的字符串填充的原因:

a = ["3", "5", "8", "4", "1", "2", "9", "10", "7", "6"]

Rather than numbers like this:

而不是这样的数字:

a = [3, 5, 8, 4, 1, 2, 9, 1, 7, 6]

?

?

回答by Thom

The cheap way would be to zero fill on the left and make all numbers 2 digit.

便宜的方法是在左侧填充零并使所有数字为 2 位。