Print array values based in sorting list and ascending order

Viewed 292

Please your help. I have the array "values" that I want to sort by 2 conditions.

First condition: Using the sort list defined in array "sortlist". Second condition: From smallest to largest.

With my current script I've been able to print the values in correct order for first condition (sort list), but I don't have an idea how to apply the second sorting condition (smallest to largest).

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

UPDATE

Thank Sebastian. Excellent.

It almost work since I noticed that if the characters after "_" are not numbers the second sort is not correct. For example below output is incorrect for values of 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
 }'

2nd Update

I mean that the first sort is based on sortlist array. The second sort is ascending based in characters after "_". In this case the firt letter for Prw values are C, E, j, L and R. But the current output is C, E, L, R and j. So, j is at the end and should be after L. I hope make sense.

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 Answers
Related