[英]How to use sort_by to sort alphabetically then numerically then by special characters
我有一个数组:
arr = ["Bar", "abc", "foo", "1", "20”, "10", "_def"]
我需要先按字母顺序使用不区分大小写的排序,然后按数字顺序排列特殊字符。
我正在尝试使用sort_by
:
irb(main):071:0> arr.sort_by {|s| [s[/[0-9a-z]+/], s.to_i]}
=> ["1", "10", "20", "abc", "Bar", "_def", "foo"]
output 必须是:
arr = ["abc", "Bar", "foo", "1", “10”, “20", "_def"]
您可以先创建组,然后对组进行排序。
arr.each_with_object(Array.new(3) { Array.new }) do |word, group|
if word.match /^[A-Za-z]/
group.first
elsif word.match /^[0-9]/
group.second
else
group.third
end << word
end.flat_map{ |group| group.sort_by{ |x| x.downcase } }
#=> ["abc", "Bar", "foo", "1", "10", "20", "_def"]
从文档:
Arrays 以“元素方式”的方式进行比较;
ary
的第一个元素使用<=>
运算符与other_ary
的第一个元素进行比较,然后是每个第二个元素,等等……
您可以通过创建排序组来利用此行为:
arr = ["Bar", "abc", "foo", "1", "20", "10", "_def"]
arr.sort_by do |s|
case s
when /^[a-z]/i
[1, s.downcase]
when /^\d/
[2, s.to_i]
else
[3, s]
end
end
#=> ["abc", "Bar", "foo", "1", "10", "20", "_def"]
The first element ( 1
, 2
, 3
) defines the group's position: strings with letters on 1st position, numeric strings on 2nd position and the remaining on 3rd position. 在每个组中,元素按第二个元素排序:带字母的字符串按其小写值排序,数字字符串按其 integer 值排序,其余按其自身排序。
需要一个小基准:
require 'active_support/core_ext/array/access.rb'
require 'fruity'
ARR = ["Bar", "abc", "foo", "1", "20", "10", "_def"]
def run_demir(ary)
ary.each_with_object(Array.new(3) { Array.new }) do |word, group|
if word.match /^[A-Za-z]/
group.first
elsif word.match /^[0-9]/
group.second
else
group.third
end << word
end.flat_map{ |group| group.sort_by{ |x| x.downcase } }
end
def run_stefan(ary)
ary.sort_by do |s|
case s
when /^[a-z]/i
[1, s.downcase]
when /^\d/
[2, s.to_i]
else
[3, s]
end
end
end
run_demir(ARR) # => ["abc", "Bar", "foo", "1", "10", "20", "_def"]
run_stefan(ARR) # => ["abc", "Bar", "foo", "1", "10", "20", "_def"]
compare do
demir { run_demir(ARR) }
Stefan { run_stefan(ARR) }
end
结果是:
# >> Running each test 512 times. Test will take about 1 second.
# >> Stefan is faster than demir by 2x ± 0.1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.