Trouble in Array sort?

Hi

 I have very silly doubt in Array and hash?

My hash value is –

‘community’=>{“108”=>"", “94”=>“12345204”,
“95”=>“[email protected]”, “96”=>“blore”}

After that i have taken only keys from this hash
params[:community].keys
Result I am getting as Arry—> [“108”, “94”, “95”, “96”]

Can I use [“108”, “94”, “95”, “96”].sort

Its not working for me…

Because I want only key value with sorted order…

your array contains strings, and when you sort strings, the output
will be [“108”, “94”, “95”, “96”]
but when you convert array elements into integers and sort after that,
you must get [94,95,96,108]

Thanks Ivanov,

Its working fine.

Actually I convert each element into integer,Then i form arry with
thiose element.

Ilja Ivanov wrote:

your array contains strings, and when you sort strings, the output
will be [“108”, “94”, “95”, “96”]
but when you convert array elements into integers and sort after that,
you must get [94,95,96,108]

Here’s how you could do it:

params[:community].keys.sort_by{|k| k.to_i}

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs