ruby 对合并在一起的数组进行排序

ia2d9nvy  于 2023-01-25  发布在  Ruby
关注(0)|答案(1)|浏览(168)

我有两个字符串,我把它们做成一个数组来排序,然后再转换回一个字符串,但是,在我的response. body的测试中,给定的字符串排序不同,我有一个方法,它接受两个字符串,从两个字符串中移除头,合并数组并排序,但是得到不同的结果,我怎样才能得到下面response.body字符串的期望结果

string1 = "Category Name,Code,Enabled?,Category Hidden?\nPRESENT AVAIALBLE,PRESENT AVAILABLE,No,No,\nBUG AVAILABLE,BUG,No,No,\nBUG,BUG,No,No,\nPRESENT,PRESENT,No,No\n"
string2 = "Category Name,Code,Enabled?,Category Hidden?\nBUG,BUG,No,No,\nBUG AVAILABLE,BUG,No,No,\nEXAMPLE 1,EXAMPLE 1,Yes,No,\nEXAMPLE 2,EXAMPLE 2,Yes,No,\nPRESENT AVAIALBLE,PRESENT AVAILABLE,No,No,\nPRESENT,PRESENT,No,No\n"

在插入头文件"Category Name,Code,Enabled?,Category Hidden?"之前,如何将数组排序为respons.body字符串
响应主体字符串

"Category Name,Code,Enabled?,Category Hidden?
BUG,BUG,No,No,
BUG AVAILABLE,BUG,No,No,
EXAMPLE 1,EXAMPLE 1,No,No,
EXAMPLE 2,EXAMPLE 2,Yes,No,
PRESENT,PRESENT,No,No"
PRESENT AVAIALBLE,PRESENT AVAILABLE,No,No"

我的方法输出

"Category Name,Code,Enabled?,Category Hidden?
BUG AVAILABLE,BUG,No,No,
BUG,BUG,No,No,
EXAMPLE 1,EXAMPLE 1,No,No,
EXAMPLE 2,EXAMPLE 2,Yes,No,
PRESENT AVAIALBLE,PRESENT AVAILABLE,No,No,
PRESENT,PRESENT,No,No"

我编写的方法

def merge(string1, string2)
  string1 = string1.split("\n") # Split into array.
  headers = string1.first # Get headers.
  string1.shift # Remove headers.
  string2 = string2.split("\n")[1..-1] # Remove headers.
  final = (string1 + string2).sort.unshift(headers).join("\n") + "\n" # Create merged sorted string.
end

期望的结果

"Category Name,Code,Enabled?,Category Hidden?
BUG,BUG,No,No,
BUG AVAILABLE,BUG,No,No,
EXAMPLE 1,EXAMPLE 1,No,No,
EXAMPLE 2,EXAMPLE 2,Yes,No,
PRESENT,PRESENT,No,No"
PRESENT AVAIALBLE,PRESENT AVAILABLE,No,No"
vptzau2j

vptzau2j1#

这里有三种方法。
我假设您有两个字符串:

str1 = "Category Name,Code,Enabled?,Category Hidden?\nBUG,BUG,No,No\nEXAMPLE 1,EXAMPLE 1,No,No\nPRESENT,PRESENT,No,No"
str2 = "Category Name,Code,Enabled?,Category Hidden?\nBUG AVAILABLE,BUG,No,No\nEXAMPLE 2,EXAMPLE 2,Yes,No\nPRESENT AVAILABLE,PRESENT AVAILABLE,No,No"

然后

header, *body1 = str1.split("\n")
  #=> ["Category Name,Code,Enabled?,Category Hidden?",
  #    "BUG,BUG,No,No",
  #    "EXAMPLE 1,EXAMPLE 1,No,No",
  #    "PRESENT,PRESENT,No,No"]

所以

header
  #=> "Category Name,Code,Enabled?,Category Hidden?"
body1
  #=> ["BUG,BUG,No,No",
  #    "EXAMPLE 1,EXAMPLE 1,No,No",
  #    "PRESENT,PRESENT,No,No"]

以及

_, *body2      = str2.split("\n")
  #=> ["Category Name,Code,Enabled?,Category Hidden?",
  #    "BUG AVAILABLE,BUG,No,No",
  #    "EXAMPLE 2,EXAMPLE 2,Yes,No",
  #    "PRESENT AVAILABLE,PRESENT AVAILABLE,No,No"]

所以

_ #=>  "Category Name,Code,Enabled?,Category Hidden?"
body2
  #=> ["BUG AVAILABLE,BUG,No,No",
  #    "EXAMPLE 2,EXAMPLE 2,Yes,No",
  #    "PRESENT AVAILABLE,PRESENT AVAILABLE,No,No"]

然后我们就可以计算出所需的字符串。

str = [header].concat(body1.zip(body2).flatten).join("\n")
  #=> "Category Name,Code,Enabled?,Category Hidden?\nBUG,BUG,No,No\nBUG AVAILABLE,BUG,No,No\nEXAMPLE 1,EXAMPLE 1,No,No\nEXAMPLE 2,EXAMPLE 2,Yes,No\nPRESENT,PRESENT,No,No\nPRESENT AVAILABLE,PRESENT AVAILABLE,No,No"

其在显示时如下所示。

puts str
Category Name,Code,Enabled?,Category Hidden?
BUG,BUG,No,No
BUG AVAILABLE,BUG,No,No
EXAMPLE 1,EXAMPLE 1,No,No
EXAMPLE 2,EXAMPLE 2,Yes,No
PRESENT,PRESENT,No,No
PRESENT AVAILABLE,PRESENT AVAILABLE,No,No

请参见数组#concat、数组#zip、数组#flatten和数组#join。
变量_, *body2 = str2.split("\n")中的变量_是这样命名的,以告诉读者它在随后的计算中不被使用。有时可能写_header, *body2 = str2.split("\n")来传达同样的信息。
下面是第二种方法,将字符串视为逗号分隔的CSV字符串。

require 'csv'
arr1 = CSV.parse(str1)​
  #=> [["Category Name", "Code", "Enabled?", "Category Hidden?"],
  #    ["BUG", "BUG", "No", "No"],
  #    ["EXAMPLE 1", "EXAMPLE 1", "No", "No"],
  #    ["PRESENT", "PRESENT", "No", "No"]],

arr2 = CSV.parse(str2)
  #=> [["Category Name", "Code", "Enabled?", "Category Hidden?"],
  #    ["BUG AVAILABLE", "BUG", "No", "No"],
  #    ["EXAMPLE 2", "EXAMPLE 2", "Yes", "No"],
  #    ["PRESENT AVAILABLE", "PRESENT AVAILABLE", "No", "No"]]

然后

str = CSV.generate do |csv|
  csv << arr1.shift
  arr2.shift
  until arr2.empty? do
    csv << arr1.shift
    csv << arr2.shift
  end
end
  #=> "Category Name,Code,Enabled?,Category Hidden?\nBUG,BUG,No,No\nBUG AVAILABLE,BUG,No,No\nEXAMPLE 1,EXAMPLE 1,No,No\nEXAMPLE 2,EXAMPLE 2,Yes,No\nPRESENT,PRESENT,No,No\nPRESENT AVAILABLE,PRESENT AVAILABLE,No,No\n"
puts str
Category Name,Code,Enabled?,Category Hidden?
BUG,BUG,No,No
BUG AVAILABLE,BUG,No,No
EXAMPLE 1,EXAMPLE 1,No,No
EXAMPLE 2,EXAMPLE 2,Yes,No
PRESENT,PRESENT,No,No
PRESENT AVAILABLE,PRESENT AVAILABLE,No,No

参见CSV::解析和CSV::生成。
这也可以在不将字符串转换为数组、操作这些数组以形成单个数组然后将该单个数组转换回字符串的情况下完成。

arr = [str1, str2]
str_indices = 0..str1.count("\n") 
arr_indices = 0..arr.size-1
idx_begin = Array.new(arr.size, 0)

puts str_indices.each_with_object("") do |i, str|
  arr_indices.each do |j|
    idx_end = arr[j].index(/(?:\n|\z)/, idx_begin[j])
     s = arr[j][idx_begin[j]..idx_end]
     s << "\n" unless s[-1] == "\n" || (i == str_indices.last && j == arr_indices.last)
     str << s unless i.zero? && j > 0
     idx_begin[j] = idx_end + 1
  end
end
Category Name,Code,Enabled?,Category Hidden?
BUG,BUG,No,No
BUG AVAILABLE,BUG,No,No
EXAMPLE 1,EXAMPLE 1,No,No
EXAMPLE 2,EXAMPLE 2,Yes,No
PRESENT,PRESENT,No,No
PRESENT AVAILABLE,PRESENT AVAILABLE,No,No

正则表达式/(?:\n|\z)/匹配换行符(\n)或(|)字符串(\z)的结尾。
请参阅String #index的格式,该格式采用可选的第二个参数,该参数指定要开始搜索的字符串索引。

相关问题