1

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

3 Answers 3

5

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:

Student.where("id IN (?)", @students).order(:last_name)

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:

@students = Student.where("id IN (?)", @students).order(:last_name).collect(&:id)

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi...Thank you for quick reply, but I need to sort array @students=[], not order data from ActiveRecords. Example: in my array there are numbers [7,13, 15], and last name of 7 is Zorro and 13 is Anthony, 15 is Lovejoy, so, in final array should be [15,13,7]...
Obviously you would need your array to contain the data you wish to sort on if looking up said data in the database is not allowed.
2

Based on your comment, you want to do the following:

  • Take in a list of IDs of students as input
  • Return a list of IDs ordered by the student's last name in the database.

You should be able to do the following:

Student.where(:id => @ids).order(:last_name).map(&:id)

Breaking this down:

  • where(:id => @ids) only selects Students with an ID in the ID array.
  • order(:last_name) sorts the results by last name.
  • map(&:id) takes in an array of Students and returns just the ID column. Essentially, the method in brackets (which is a shortcut for calling id for each student) is called for each student found, and the return values are assembled into a new array (which will only contain the ids).

Some gotchas:

  • If an ID doesn't exist in the database, it will be excluded from the results - if your result array is smaller than your input array, you may be trying to access a record that no longer exists.
  • If the Students table has a lot of columns, you may want to consider calling select(:id) so that you don't pull every column of the Student records from the database.

Comments

0
Student.find( @students ).sort_by { |s| s.last_name }

1 Comment

This works well, but keep in mind it will do the ordering in memory, which may be inefficient if you have a large number of Student records to sort (particularly if the last_name column is indexed)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.