ruby排序数组 - 将匹配的元素移动到开头

时间:2016-06-24 18:38:22

标签: arrays ruby

如果我有一个数组:array = ["ruby", "code", "library"]。如何将匹配的/ ^ library $ / elements移动到开头。所以数组看起来像这样:array = [" library"," ruby​​"," code"]

4 个答案:

答案 0 :(得分:5)

可以通过多种方式完成。这是一个

array = ["ruby", "code", "library"]
array.partition { |element| element.match /^library$/ }.flatten

答案 1 :(得分:2)

出于好奇:

[:select, :reject].map do |m|
  ["ruby", "code", "library"].public_send(m, &(/^library/.method(:=~)))
end.reduce :| 

答案 2 :(得分:1)

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class SomeClass {
    // make sure the test file is at least this size, or you'll get a "cannot 
    // extend file to required size" exception when creating the buffer
    private static int bufferSize = 10;

    public static void main(String[] args) throws Exception {
        File file = new File("/tmp/someFile");

        FileInputStream fis = new FileInputStream(file);
        ByteBuffer bb = getByteBuffer(fis);

        // If you comment this out, the error goes away.
        new FileOutputStream(file);

        bb.get();
    }

    private static ByteBuffer getByteBuffer(FileInputStream fis) throws Exception {
        return fis.getChannel().map(FileChannel.MapMode.READ_ONLY,
            fis.getChannel().position(), bufferSize);
    }
}

注意:

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

方法matching_indices ["ruby", "libraries", "code", "library"], /librar(?:ies|y)/ #=> [1, 3] 保留移动的元素和未移动元素的顺序。

答案 3 :(得分:0)

三分钱一分。

array.inject([]){|a,e| e[/^library/] ? a.unshift(e) : a<<e}

array & ["library"] | array

如果数组包含多次搜索元素,则变为

array.find_all{ |e| e[/^library/] } + array.reject{ |e| e[/^library/] }

如果你讨厌两次使用数组变量,它也可能喜欢这个

[array].map{|a| a & ["library"] | a}.flatten

最后一个:使用grep

array.grep(/library/) + array.grep( /^(?!library)/)