dimanche 12 avril 2015

How to re-order an array of objects based on a list of ids

I have an object Task as you can see below:



class Task
include HTTParty

attr_accessor :id, :name, :assignee_status, :order

def initialize(id, name, assignee_status)
self.id = id
self.name = name
self.status = status
self.order = order
end
end


So, when I load a list of tasks, I give them a specific order, like this:



i = 0
@tasks.each do |at|
at.order = i
i += 100
end


This list is, then, sent over json to a client app which shows is to the user and allows for drag'n'drop. After some task is re-ordered, the front-end app sends a list back to the server with all the task ids in the new order.


Example: 23,45,74,22,11,98,23


What I want to do is the re-order the array of objects based on the ids that I found. What I thought about doing was to set all order to 0 and then search each object individually on the matrix and set their priority accordingly. But this just feels wrong... feels to computing intensive.


Is there a clever way to do something like:


ArrayOfTaskObjects.orderbyAttribute(id) ??


Aucun commentaire:

Enregistrer un commentaire