Forum: Ruby on Rails Sorting an array of string-integer identifiers

Posted by Avantec Van (avantec)
on 2012-12-07 16:53
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
Posted by Walter Davis (walterdavis)
on 2012-12-07 17:02
(Received via mailing list)
On Dec 7, 2012, at 10:53 AM, Avantec Van wrote:

> ["question_7_icd9px_1", ""], ["question_7_sdx_1", "2"],
> ["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
Posted by Avantec Van (avantec)
on 2012-12-07 18:04
Walter Davis wrote in post #1088232:
> On Dec 7, 2012, at 10:53 AM, Avantec Van wrote:

>
> 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"}
Posted by Walter Davis (walterdavis)
on 2012-12-07 18:09
(Received via mailing list)
On Dec 7, 2012, at 12:04 PM, Avantec Van wrote:

>> Walter
> "answer_modifier_8_cpt_2"=>"", "question_8_sdx_2"=>"",
> "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
Posted by Ignacio Piantanida (Guest)
on 2012-12-07 22:44
(Received via mailing list)
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

--
Piantanida, Ignacio Julin



2012/12/7 Walter Lee Davis <waltd@wdstudio.com>
Posted by Avantec Van (avantec)
on 2012-12-08 10:53
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
Posted by Walter Davis (walterdavis)
on 2012-12-08 16:38
(Received via mailing list)
On Dec 8, 2012, at 4:53 AM, Avantec Van wrote:

>    [$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
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.