I have one array, which are IDs from one ActiveRecord table.
so I would like to sort that array based on last name which is associated with that ID…how can I do that?
To clarify:
array @students=[], inside are IDs and I would like to sort by Student.find(ID).last
Thank you.
Dorijan
I have one array, which are IDs from one ActiveRecord table. so I would
Share
Without fully understanding the question, if you’re given a list of id’s, you can sort by last_name when you’re doing the query:
Of course, this assumes that @students is an array of ids and nothing else.
Responding to your comment, I’m not sure why you’d need to do that, but if your @student array is just a list of ids ignorant of the Student model and its attributes, and you would still like to order that array, you can do this:
This will return an array of ids sorted by last name. But again, I don’t really know what you have going on behind the scenes, and I’m not sure what you’re asking for.