根据字符串的整数值对数组进行排序

时间:2022-12-26 19:16:05

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!

所以字符串不能被转换成整数。我写问题的时候应该把这个写出来。我不认为修复会有很大的不同。我的错误,我为做出这样的假设而道歉!如何排序这个数组?谢谢!

6 个解决方案

#1


45  

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

我再讲一个方法,因为这是我能想到的最短的方法

a.sort_by(&:to_i)

#2


8  

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(&:+)}

#3


6  

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

#4


6  

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]

#5


1  

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]

?

吗?

#6


0  

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

最便宜的方法是在左边填零,然后把所有数字都变成两位数。

#1


45  

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

我再讲一个方法,因为这是我能想到的最短的方法

a.sort_by(&:to_i)

#2


8  

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(&:+)}

#3


6  

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

#4


6  

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]

#5


1  

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]

?

吗?

#6


0  

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

最便宜的方法是在左边填零,然后把所有数字都变成两位数。