Sorting an array of string-integer identifiers

Hi All,
   I have an array like this.

[["question_7_pdx_0", ""], ["question_7_sdx_3", "4"],
["question_7_cpt_2", ""], ["question_7_icd9px_0", ""],
["question_7_sdx_0", "1"], ["question_7_sdx_8", ""],
["question_7_sdx_11", ""], ["question_7_sdx_5", ""],
["question_7_icd9px_2", ""], ["question_7_sdx_2", "3"],
["question_7_cpt_1", ""], ["question_7_adx_0", ""], ["question_7_sdx_7",
""], ["question_7_sdx_10", ""], ["question_7_sdx_4", ""],
["question_7_icd9px_1", ""], ["question_7_sdx_1", "2"],
["question_7_cpt_0", ""], ["question_7_sdx_9", ""],
["question_7_sdx_12", ""], ["question_7_sdx_6", ""]]

I want this array to sort like this.

[["question_7_adx_0", ""], ["question_7_cpt_0", ""],
["question_7_cpt_1", ""], ["question_7_cpt_2", ""],
["question_7_icd9px_0", ""], ["question_7_icd9px_1", ""],
["question_7_icd9px_2", ""], ["question_7_pdx_0", ""],
["question_7_sdx_0", "1"], ["question_7_sdx_1", "2"],
["question_7_sdx_2", "3"], ["question_7_sdx_3", "4"],
["question_7_sdx_4", ""], ["question_7_sdx_5", ""], ["question_7_sdx_6",
""], ["question_7_sdx_7", ""], ["question_7_sdx_8", ""],
["question_7_sdx_9", ""], ["question_7_sdx_10", ""],
["question_7_sdx_11", ""], ["question_7_sdx_12", ""]]

Please help me.

Thanks in advance,
Avantec

Hi All,
  I have an array like this.

[["question_7_pdx_0", ""], ["question_7_sdx_3", "4"],
["question_7_cpt_2", ""], ["question_7_icd9px_0", ""],
["question_7_sdx_0", "1"], ["question_7_sdx_8", ""],
["question_7_sdx_11", ""], ["question_7_sdx_5", ""],
["question_7_icd9px_2", ""], ["question_7_sdx_2", "3"],
["question_7_cpt_1", ""], ["question_7_adx_0", ""], ["question_7_sdx_7",
""], ["question_7_sdx_10", ""], ["question_7_sdx_4", ""],
["question_7_icd9px_1", ""], ["question_7_sdx_1", "2"],
["question_7_cpt_0", ""], ["question_7_sdx_9", ""],
["question_7_sdx_12", ""], ["question_7_sdx_6", ""]]

I want this array to sort like this.

[["question_7_adx_0", ""], ["question_7_cpt_0", ""],
["question_7_cpt_1", ""], ["question_7_cpt_2", ""],
["question_7_icd9px_0", ""], ["question_7_icd9px_1", ""],
["question_7_icd9px_2", ""], ["question_7_pdx_0", ""],
["question_7_sdx_0", "1"], ["question_7_sdx_1", "2"],
["question_7_sdx_2", "3"], ["question_7_sdx_3", "4"],
["question_7_sdx_4", ""], ["question_7_sdx_5", ""], ["question_7_sdx_6",
""], ["question_7_sdx_7", ""], ["question_7_sdx_8", ""],
["question_7_sdx_9", ""], ["question_7_sdx_10", ""],
["question_7_sdx_11", ""], ["question_7_sdx_12", ""]]

Please help me.

The first question I would have is, how did you get this array in the first place? If it's the output from an ActiveRecord query, then you could most profitably sort there in the data-gathering stage. Otherwise, if you redefine your sort method (or use a custom sort method) then you should be able to sort by the value of index 0 of each sub-array, right?

Walter

Walter Davis wrote in post #1088232:

The first question I would have is, how did you get this array in the
first place? If it's the output from an ActiveRecord query, then you
could most profitably sort there in the data-gathering stage. Otherwise,
if you redefine your sort method (or use a custom sort method) then you
should be able to sort by the value of index 0 of each sub-array, right?

Walter

Hi Walter,
  Thanks for your reply.
I'm getting this as params and I'm just making this into array.

my params is like this {"question_8_pdx_0"=>"", "question_7_sdx_8"=>"9",
"question_8_cpt_1"=>"", "course_id"=>"1", "answer_modifier_7_cpt_1"=>"",
"answer_quantity_8_cpt_0"=>"", "question_7_sdx_5"=>"6",
"question_7_cpt_2"=>"", "question_7_sdx_10"=>"11",
"question_8_icd9px_1"=>"", "question_7_adx_0"=>"298.9",
"answer_modifier_8_cpt_2"=>"", "question_8_sdx_2"=>"",
"question_7_icd9px_1"=>"", "question_7_sdx_2"=>"3",
"answer_quantity_8_cpt_2"=>"", "answer_quantity_7_cpt_1"=>"",
"question_7_sdx_7"=>"8", "user_id"=>"1", "question_8_cpt_0"=>"",
"context_type"=>"Course", "answer_modifier_7_cpt_0"=>"",
"question_7_sdx_4"=>"5", "question_7_cpt_1"=>"",
"question_8_icd9px_0"=>"", "question_8_sdx_1"=>"",
"question_7_icd9px_0"=>"", "question_7_sdx_1"=>"2",
"question_7_pdx_0"=>"2323", "question_7_sdx_9"=>"10",
"answer_quantity_7_cpt_0"=>"", "answer_modifier_7_cpt_2"=>"",
"question_7_sdx_6"=>"7", "context_id"=>"1", "question_7_sdx_11"=>"",
"question_8_icd9px_2"=>"", "question_7_icd9px_2"=>"",
"question_7_sdx_3"=>"4", "question_7_cpt_0"=>"",
"answer_modifier_8_cpt_0"=>"", "question_8_adx_0"=>"",
"question_8_sdx_0"=>"", "answer_quantity_7_cpt_2"=>"",
"question_7_sdx_0"=>"1"}

Walter Davis wrote in post #1088232:

The first question I would have is, how did you get this array in the
first place? If it's the output from an ActiveRecord query, then you
could most profitably sort there in the data-gathering stage. Otherwise,
if you redefine your sort method (or use a custom sort method) then you
should be able to sort by the value of index 0 of each sub-array, right?

Walter

Hi Walter,
Thanks for your reply.
I'm getting this as params and I'm just making this into array.

my params is like this {"question_8_pdx_0"=>"", "question_7_sdx_8"=>"9",
"question_8_cpt_1"=>"", "course_id"=>"1", "answer_modifier_7_cpt_1"=>"",
"answer_quantity_8_cpt_0"=>"", "question_7_sdx_5"=>"6",
"question_7_cpt_2"=>"", "question_7_sdx_10"=>"11",
"question_8_icd9px_1"=>"", "question_7_adx_0"=>"298.9",
"answer_modifier_8_cpt_2"=>"", "question_8_sdx_2"=>"",
"question_7_icd9px_1"=>"", "question_7_sdx_2"=>"3",
"answer_quantity_8_cpt_2"=>"", "answer_quantity_7_cpt_1"=>"",
"question_7_sdx_7"=>"8", "user_id"=>"1", "question_8_cpt_0"=>"",
"context_type"=>"Course", "answer_modifier_7_cpt_0"=>"",
"question_7_sdx_4"=>"5", "question_7_cpt_1"=>"",
"question_8_icd9px_0"=>"", "question_8_sdx_1"=>"",
"question_7_icd9px_0"=>"", "question_7_sdx_1"=>"2",
"question_7_pdx_0"=>"2323", "question_7_sdx_9"=>"10",
"answer_quantity_7_cpt_0"=>"", "answer_modifier_7_cpt_2"=>"",
"question_7_sdx_6"=>"7", "context_id"=>"1", "question_7_sdx_11"=>"",
"question_8_icd9px_2"=>"", "question_7_icd9px_2"=>"",
"question_7_sdx_3"=>"4", "question_7_cpt_0"=>"",
"answer_modifier_8_cpt_0"=>"", "question_8_adx_0"=>"",
"question_8_sdx_0"=>"", "answer_quantity_7_cpt_2"=>"",
"question_7_sdx_0"=>"1"}

Okay, then what does the order of these params have to do with anything? If you are updating existing attributes, then you don't need to worry about what order you post those variables to the controller. If you're creating a new record, then likewise, the order of the attributes doesn't impact the result of a save on that record.

Walter

Try this:

my_array.sort do |item1, item2|
first_1=item1.first
first_2=item2.first
regex = /\w*\d*(\w*)_(\d*)/
first_1 =~ regex
letters_1 = $1
numbers_1 = $2.to_i
first_2 =~ regex

letters_2 = $1
numbers_2 = $2.to_i
if letters_1 == letters_2
numbers_1 <=> numbers_2
else
letters_1 <=> letters_2
end
end

I got the answer.

Wrote like this

array.sort_by {|i| str_to_num(i[0]) } &

  def str_to_num(str)
    code = str.split('_')[2]
    regex = Regexp.new "^question_(\\d+)_#{code}_(\\d+)$"
    str =~ regex
    [$1.to_i, $2.to_i]
  end

Thanks
Avantec

I'm still curious why you needed these parameters to be in a particular order. It seems to be unusual in the Rails form-handling patterns to have this matter at all. It's a hash, and those don't have an explicit order anyway (until Ruby 1.9.3).

Walter