簡體   English   中英

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

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM