如何在 ruby 中对字母数字数组进行排序
如何在 ruby 中按字母数字顺序对数组数据进行排序?
假设我的数组是 a = [test_0_1, test_0_2, test_0_3, test_0_4, test_0_5, test_0_6, test_0_7, test_0_8, test_0_9, test_1_0, test_1_1, test_1_2, test_1_3, test_1_4, test_1_5, test_1_6, test_1_7, test_1_8, 、测试_1_10、 test_1_11, test_1_12, test_1_13, test_1_14, ..........test_1_121.........................]
我希望我的输出是:
.
.
.
test_1_121
.
.
.
test_1_14
test_1_13
test_1_12
test_1_11
test_1_10
test_1_9
test_1_8
test_1_7
test_1_6
test_1_5
test_1_4
test_1_3
test_1_2
test_1_1
test_0_10
test_0_9
test_0_8
test_0_7
test_0_6
test_0_5
test_0_4
test_0_3
test_0_2
test_0_1
How I can sort array data alphanumerically in ruby?
Suppose my array is a = [test_0_1, test_0_2, test_0_3, test_0_4, test_0_5, test_0_6, test_0_7, test_0_8, test_0_9, test_1_0, test_1_1, test_1_2, test_1_3, test_1_4, test_1_5, test_1_6, test_1_7, test_1_8, test_1_9, test_1_10, test_1_11, test_1_12, test_1_13, test_1_14, ...........test_1_121...............]
I want my output to be:
.
.
.
test_1_121
.
.
.
test_1_14
test_1_13
test_1_12
test_1_11
test_1_10
test_1_9
test_1_8
test_1_7
test_1_6
test_1_5
test_1_4
test_1_3
test_1_2
test_1_1
test_0_10
test_0_9
test_0_8
test_0_7
test_0_6
test_0_5
test_0_4
test_0_3
test_0_2
test_0_1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
一种通用算法,用于对在任意位置包含非填充序列号的字符串进行排序。
其中 padding 是您希望数字在比较期间具有的字段长度。字符串中找到的任何数字如果包含少于“填充”数量的数字,则在比较之前将用零填充,从而产生预期排序顺序。
要产生用户682932要求的结果,只需在排序块后添加
.reverse
,这会将自然排序(升序)翻转为降序。通过对字符串进行预循环,您当然可以动态地找到字符串列表中的最大位数,您可以使用它来代替硬编码一些任意填充长度,但这将需要更多处理(更慢)和更多代码。例如
A generic algorithm for sorting strings that contain non-padded sequence numbers at arbitrary positions.
where padding is the field length you want the numbers to have during comparison. Any number found in a string will be zero padded before comparison if it consists of less than "padding" number of digits, which yields the expected sorting order.
To yield the result asked for by the user682932, simply add
.reverse
after the sort block, which will flip the natural ordering (ascending) into a descending order.With a pre-loop over the strings you can of course dynamically find the maximum number of digits in the list of strings, which you can use instead of hard-coding some arbitrary padding length, but that would require more processing (slower) and a bit more code. E.g.
例如,如果您只是按字符串排序,则将无法获得“test_2”和“test_10”之间的正确顺序。所以这样做:
If you simply sort as string, you will not get the correct ordering between 'test_2' and 'test_10', for example. So do:
您可以将块传递给排序函数以对其进行自定义排序。在您的情况下,您会遇到问题,因为您的数字没有用零填充,因此此方法对数字部分进行零填充,然后对它们进行排序,从而得到您想要的排序顺序。
You can pass a block to the sort function to custom sort it. In your case you will have a problem because your numbers aren't zero padded, so this method zero pads the numerical parts, then sorts them, resulting in your desired sort order.
排序例程的处理时间可能差异很大。排序的基准测试变体可以快速找到最快的做事方式:
及其输出:
测试算法的速度显示:
Sort1 和 sort2 以及 sort_by1 和 sort_by2 帮助建立
sort
的基线,sort_by
以及两个带有reverse
的排序。排序 sort3 和 sort_by3 是此页面上的另外两个答案。 Sort_by4 和 sort_by5 是我如何做的两个旋转,sort_by5 是我经过几分钟的修改后想出的最快的。
这显示了算法中的微小差异如何对最终输出产生影响。如果有更多的迭代,或者对更大的数组进行排序,则差异会更加极端。
Sort routines can have greatly varying processing times. Benchmarking variations of the sort can quickly home in on the fastest way to do things:
And its output:
Testing the algorithms for speed shows:
Sort1 and sort2 and sort_by1 and sort_by2 help establish baselines for
sort
,sort_by
and both of those withreverse
.Sorts sort3 and sort_by3 are two other answers on this page. Sort_by4 and sort_by5 are two spins on how I'd do it, with sort_by5 being the fastest I came up with after a few minutes of tinkering.
This shows how minor differences in the algorithm can make a difference in the final output. If there were more iterations, or larger arrays being sorted the differences would be more extreme.
与 @ctcherry 答案类似,但速度更快:
编辑:我的测试:
输出:
Similar to @ctcherry answer, but faster:
EDIT: My tests:
Output:
在此发布一种在 Ruby 中执行自然十进制排序的更通用方法。
以下内容受到我的“像 Xcode”排序代码的启发,来自 https://github.com/CocoaPods/Xcodeproj/blob/ca7b41deb38f43c14d066f62a55edcd53876cd07/lib/xcodeproj/project/object/helpers/sort_helper.rb,本身松散地受到 https://rosettacode.org/wiki/Natural_sorting#Ruby。
即使很明显我们希望“10”位于“2”之后进行自然十进制排序,但对于所需的多种可能的替代行为,还需要考虑其他方面:
考虑到这些因素:
scan
而不是split
,因为我们可能会比较三种子字符串(数字、空格、所有其他)。Comparable
类和def <=>(other)
,因为不可能简单地map
> 每个子字符串到其他东西,根据上下文(第一遍和相等遍),这些东西将有两种不同的行为。这会导致实现有点冗长,但它非常适合边缘情况:
使用
示例 1:
示例 2:
您可以在 https://github.com/CocoaPods/Xcodeproj/blob/ca7b41deb38f43c14d066f62a55edcd53876cd07/spec/project/object/helpers/sort_helper_spec.rb,我使用此参考进行订购:
[
'一',
'一',
'0.1.1',
'0.1.01',
'0.1.2',
'0.1.10',
'1',
'01',
'1a',
'2',
'2a',
'10',
'一个',
'一个',
'一',
'a 2',
'a1',
'A1B001',
'A01B1',
】
当然,现在就可以随意定制您自己的排序逻辑。
Posting here a more general way to perform a natural decimal sort in Ruby.
The following is inspired by my code for sorting "like Xcode" from https://github.com/CocoaPods/Xcodeproj/blob/ca7b41deb38f43c14d066f62a55edcd53876cd07/lib/xcodeproj/project/object/helpers/sort_helper.rb, itself loosely inspired by https://rosettacode.org/wiki/Natural_sorting#Ruby.
Even if it's clear that we want "10" to be after "2" for a natural decimal sort, there are other aspects to consider with multiple possible alternative behaviors wanted:
With those considerations:
scan
instead ofsplit
, because we're going to have potentially three kinds of substrings to compare (digits, spaces, all-the-rest).Comparable
class and withdef <=>(other)
because it's not possible to simplymap
each substring to something else that would have two distinct behaviors depending on context (the first pass and the equality pass).This results in a bit lengthy implementation, but it works nicely for edge situations:
Usage
Example 1:
Example 2:
You may find my test case at https://github.com/CocoaPods/Xcodeproj/blob/ca7b41deb38f43c14d066f62a55edcd53876cd07/spec/project/object/helpers/sort_helper_spec.rb, where I used this reference for ordering:
[
' a',
' a',
'0.1.1',
'0.1.01',
'0.1.2',
'0.1.10',
'1',
'01',
'1a',
'2',
'2 a',
'10',
'a',
'A',
'a ',
'a 2',
'a1',
'A1B001',
'A01B1',
]
Of course, feel free to customize your own sorting logic now.
我检查了 Unix
sort
函数的 Wikipedia 页面,该函数的 GNU 版本有一个-V
标志,可以对“版本字符串”进行一般排序。 (我认为这意味着数字和非数字的混合,您希望数字部分按数字排序,非数字部分按词法排序)。文章指出:
sawa 的解决方案有点像这样,但不按非数字部分排序。
因此,在 Coeur 和 sawa 的,其工作方式类似于 GNU
sort -V
在我的例子中,我想按这样的字段对 TSV 文件进行排序,所以作为奖励,这也是这种情况的脚本:(
旁白:另一个解决方案这里 使用
Gem::Version
进行排序,但这似乎只适用于格式良好的 Gem 版本字符串。)I checked the Wikipedia page for the Unix
sort
function, the GNU version of which has a-V
flag which sorts "version strings" generically. (I take this to mean mixtures of digits and non-digits, where you want the numeric parts to be sorted numerically, and the non-numeric parts lexically).The article states that:
sawa's solution works somewhat like this but doesn't sort by non-numeric parts.
So it seems useful to post a solution somewhere between Coeur and sawa's, which works like GNU
sort -V
In my case, I wanted to sort a TSV file by its fields like this, so as a bonus, here's the script for this case too:
(Aside: another solution here sorts using
Gem::Version
, but that only seems to work with well formed Gem version strings.)从表面上看,您想要使用 排序函数和/或反向函数。
From the looks of it, you want to use the sort function and/or the reverse function.
好吧,从你的输出来看,你似乎只是想让它反转,所以使用
reverse()
Ok, from your output , it seems like you just want it to reverse, so use
reverse()