打印基于排序列表和升序的数组值

时间:2017-07-19 20:50:09

标签: arrays ruby sorting multiple-conditions

请你的帮助。我有数组“值”,我想按2个条件排序。

第一个条件:使用数组“sortlist”中定义的排序列表。 第二个条件:从最小到最大。

使用我当前的脚本,我已经能够以正确的顺序打印第一个条件(排序列表)的值,但我不知道如何应用第二个排序条件(从最小到最大)。

ruby -e'
values = [ "Ghu_1","Prw_1","Prw_3","Prw_5","Vep_3","Hom_2","Vep_1","Hom_1","Prw_2","Vep_2","Prw_4" ]
sortlist = [ "Hom","Vep","Ghu","Prw" ]

sortlist.each{ |s| 
 values.each{ |v| 
     puts v if v.include?(s)
 }
}'




Current Ouput  #  Desired output
   Hom_2       #      Hom_1
   Hom_1       #      Hom_2
   Vep_3       #      Vep_1
   Vep_1       #      Vep_2
   Vep_2       #      Vep_3
   Ghu_1       #      Ghu_1
   Prw_1       #      Prw_1
   Prw_3       #      Prw_2
   Prw_5       #      Prw_3
   Prw_2       #      Prw_4
   Prw_4       #      Prw_5

更新

感谢塞巴斯蒂安。优秀。

它几乎起作用,因为我注意到如果“_”之后的字符不是数字,则第二种不正确。例如,下面的输出对于Pwr_XXX

的值不正确
 ruby -e'
 values = [ "Ghu_Klca","Prw_Rkdg","Prw_Ceteu","Prw_Eriir","Vep_Msas23","Hom_Ttgr5","Vep_Qsccas","Hom_Ftjh","Prw_jpolq","Vep_Szbqh","Prw_Lmnajh" ]
 sortlist = [ "Hom","Vep","Ghu","Prw" ]

 puts sortlist.flat_map{ |s|
   values.select{ |v|
     v if v.include?(s)
   }.sort
 }'

第二次更新

我的意思是第一种排序基于sortlist数组。第二种是在“_”之后以字符升序。在这种情况下,Prw值的字母是 C,E,j,L和R.但是电流输出是C,E,L,R和j。所以,j在最后,应该在L之后。我希望有意义。

Current output        Expected output
Hom_Ftjh          #      Hom_Ftjh 
Hom_Ttgr5         #      Hom_Ttgr5 
Vep_Msas23        #      Vep_Msas23 
Vep_Qsccas        #      Vep_Qsccas 
Vep_Szbqh         #      Vep_Szbqh 
Ghu_Klca          #      Ghu_Klca 
Prw_Ceteu         #      Prw_Ceteu 
Prw_Eriir         #      Prw_Eriir 
Prw_Lmnajh        #      Prw_jpolq 
Prw_Rkdg          #      Prw_Lmnajh 
Prw_jpolq         #      Prw_Rkdg

2 个答案:

答案 0 :(得分:2)

查看你的代码,你可以做的是创建一个空数组,然后在你的values.each迭代中,如果该元素包含来自{{1}的元素,则将每个元素推送到空数组}

sortlist

然后使用new_list = [] sortlist.each{ |s| values.each{ |v| new_list << v if v.include?(s) } } 按第一个字符对它们进行分组:

group_by

然后使用p new_list.group_by{|e| e[0]} # => {"H"=>["Hom_2", "Hom_1"], "V"=>["Vep_3", "Vep_1", "Vep_2"], "G"=>["Ghu_1"], "P"=>["Prw_1", "Prw_3", "Prw_5", "Prw_2", "Prw_4"]} 获取包含所有值的“普通”数组,但在迭代内对哈希中每个键的每个值进行排序:

flat_map

p new_list.group_by{|e| e[0]}.flat_map{|_,v| v.sort} # => ["Hom_1", "Hom_2", "Vep_1", "Vep_2", "Vep_3", "Ghu_1", "Prw_1", "Prw_2", "Prw_3", "Prw_4", "Prw_5"] flat_mapselect组合(第二次更新):

sort_by

排序时使用values = [ "Ghu_Klca","Prw_Rkdg","Prw_Ceteu","Prw_Eriir","Vep_Msas23","Hom_Ttgr5","Vep_Qsccas","Hom_Ftjh","Prw_jpolq","Vep_Szbqh","Prw_Lmnajh" ] sortlist = [ "Hom","Vep","Ghu","Prw" ] p sortlist.flat_map{ |s| values.select{ |v| v if v.include?(s) }.sort_by(&:downcase) } # => ["Hom_Ftjh", "Hom_Ttgr5", "Vep_Msas23", "Vep_Qsccas", "Vep_Szbqh", "Ghu_Klca", "Prw_Ceteu", "Prw_Eriir", "Prw_jpolq", "Prw_Lmnajh", "Prw_Rkdg"] “忽略”大小写。

答案 1 :(得分:2)

values.sort_by { |val|
  front, back = val.split('_')
  [sortlist.index(front), back.downcase]
}
# => ["Hom_1", "Hom_2", "Vep_1", "Vep_2", "Vep_3", "Ghu_1", "Prw_1", "Prw_2", "Prw_3", "Prw_4", "Prw_5"]
# or
# => ["Hom_Ftjh", "Hom_Ttgr5", "Vep_Msas23", "Vep_Qsccas", "Vep_Szbqh", "Ghu_Klca", "Prw_Ceteu", "Prw_Eriir", "Prw_Lmnajh", "Prw_Rkdg", "Prw_jpolq"]

这是有效的,因为数组通过比较第一个元素进行比较,然后比较下一个元素(如果前一个元素相等)。目前我正在比较back,好像它是一个字符串(102之前);如果您想要数字排序(2来自10),请使用back.to_iback.to_f(但不会使用第二个示例)。