[英]ruby sorting array - moving matched elements to the beginning
If I have an array: array = ["ruby", "code", "library"]
. 如果我有一个数组: array = ["ruby", "code", "library"]
。 How can I move matched /^library$/ elements to the beginning. 如何将匹配的/ ^ library $ / elements移动到开头。 So array will look like this: array = ["library", "ruby", "code"] 所以数组看起来像这样:array = [“library”,“ruby”,“code”]
it could be done in a number of ways. 它可以通过多种方式完成。 This is one 这是一
array = ["ruby", "code", "library"]
array.partition { |element| element.match /^library$/ }.flatten
Just out of curiosity: 只是出于好奇:
[:select, :reject].map do |m|
["ruby", "code", "library"].public_send(m, &(/^library/.method(:=~)))
end.reduce :|
def move_to_front(arr, pattern)
mi = matching_indices(arr, pattern)
return arr unless mi
a = arr.dup
mi.reverse_each.with_object([]) { |i,b| b.unshift(a.delete_at(i)) }.concat(a)
end
def matching_indices(arr, pattern)
arr.each_index.select do |i|
case pattern
when Regexp then arr[i] =~ pattern
when Proc then pattern[arr[i]]
else (arr[i] == pattern)
end
end
end
move_to_front ["ruby", "code", "library"], /\Alibrary\z/
#=> ["library", "ruby", "code"]
move_to_front ["ruby", "library", "code", "library"], "library"
#=> ["library", "library", "ruby", "code"]
move_to_front ["ruby", "libraries", "code", "library"], /librar(?:ies|y)/
#=> ["libraries", "library", "ruby", "code"]
move_to_front ["ruby", "libraries", "code", "library"], /\Alibrar/
#=> ["libraries", "library", "ruby", "code"]
move_to_front ["ruby", "libraries", "code", "library"],
->(str) { str =~ /librar(?:ies|y)/ }
#=> ["libraries", "library", "ruby", "code"]
move_to_front ("1".."9").to_a, /[13579]/
#=> ["1", "3", "5", "7", "9", "2", "4", "6", "8"]
move_to_front ("1".."9").to_a, ->(n) { n.to_i.odd? }
#=> ["1", "3", "5", "7", "9", "2", "4", "6", "8"]
move_to_front ("1".."9").to_a, ->(n) { false }
#=> ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
move_to_front ("1".."9").to_a, ->(n) { true }
#=> ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
Note: 注意:
matching_indices ["ruby", "libraries", "code", "library"], /librar(?:ies|y)/
#=> [1, 3]
The method move_to_front
preserves the order of those elements that are moved and those that are not moved. 方法move_to_front
保留移动的元素和未移动的元素的顺序。
Three for one cent. 三分钱一分。
array.inject([]){|a,e| e[/^library/] ? a.unshift(e) : a<<e}
and 和
array & ["library"] | array
In case array contains the search element multiple times it becomes 如果数组包含多次搜索元素,则变为
array.find_all{ |e| e[/^library/] } + array.reject{ |e| e[/^library/] }
If you hate to use the array variable twice it can also like this 如果你讨厌两次使用数组变量,它也可以这样
[array].map{|a| a & ["library"] | a}.flatten
The last one: using grep 最后一个:使用grep
array.grep(/library/) + array.grep( /^(?!library)/)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.