Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort a ruby hashmap

I have the following line of code

<% map = options_for_select(User.all.map {|u| [u.first_name+" "+u.last_name, u.id]}) %>

which grabs the first and last name of a user and submits its ID in a form. Now I have added a few users and they are not in alphabetical order. How can I sort this map by first name?

like image 481
Killerpixler Avatar asked Jun 16 '26 05:06

Killerpixler


1 Answers

You can also use order to get the rows already ordered from the database:

<% map = options_for_select(User.all.order(:first_name).map {|u| [u.first_name+" "+u.last_name, u.id]}) %>
like image 74
Uri Agassi Avatar answered Jun 19 '26 00:06

Uri Agassi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!