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 Julián
2012/12/7 Walter Lee Davis <[email protected]>
>
> On Dec 7, 2012, at 12:04 PM, Avantec Van wrote:
>
> > 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"}
> >
>
> 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 via http://www.ruby-forum.com/.
> >
> > --
> > You received this message because you are subscribed to the Google
> Groups "Ruby on Rails: Talk" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> [email protected].
> > For more options, visit https://groups.google.com/groups/opt_out.
> >
> >
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit https://groups.google.com/groups/opt_out.