简体   繁体   English

如何使用 sort_by 按字母顺序排序,然后按数字排序,然后按特殊字符排序

[英]How to use sort_by to sort alphabetically then numerically then by special characters

I have an array:我有一个数组:

arr = ["Bar", "abc", "foo", "1", "20”, "10", "_def"]

I need to sort using case-insensitive alphabetically first, then numerically followed by special characters.我需要先按字母顺序使用不区分大小写的排序,然后按数字顺序排列特殊字符。

I am trying to use sort_by :我正在尝试使用sort_by

irb(main):071:0> arr.sort_by {|s| [s[/[0-9a-z]+/], s.to_i]}
=> ["1", "10", "20", "abc", "Bar", "_def", "foo"]

The output has to be: output 必须是:

arr = ["abc", "Bar", "foo", "1", “10”, “20", "_def"]

You can create groups first and then sort groups.您可以先创建组,然后对组进行排序。

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"]

From the docs :文档

Arrays are compared in an “element-wise” manner; Arrays 以“元素方式”的方式进行比较; the first element of ary is compared with the first one of other_ary using the <=> operator, then each of the second elements, etc… ary的第一个元素使用<=>运算符与other_ary的第一个元素进行比较,然后是每个第二个元素,等等……

You can take advantage of this behavior by creating sorting groups:您可以通过创建排序组来利用此行为:

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. 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. Within each group, the elements are sorted by the second element: strings with letters by their lowercase value, numeric strings by their integer value and the remaining by themselves.在每个组中,元素按第二个元素排序:带字母的字符串按其小写值排序,数字字符串按其 integer 值排序,其余按其自身排序。

A little benchmark is needed:需要一个小基准:

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

Which results in:结果是:

# >> 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM