Topic: Sorting Multiple Nested Lists.

So have an application that has nested sortable lists 3 deep something like so:

1. Releases 1
1.1 Features 1
1.1.1 Task 1
1.2 Features 2
1.2.1 Task 2
1.2.2 Task 3
2. Release 2
2.1 Feature 3
2.1.1 Task 4


But you can resort the lists so that a feature can be reordered to belong to any release, and a task can be moved to any feature.

After much work I have this working on the front end and sending an AJAX  request that will look like so, after moving a feature you get a post with the following key/value pairs:

features_for_release_8[] =    3
features_for_release_8[] =    2
features_for_release_9[] =    1

My question is the best way to structure my method for sorting these?  Note the _8[] "8" is the id of the release, so it would be release 8 has the following tasks in the following order
Release 8
1. Feature id 3.
2. Feature id 2.
Release 9
1. Feature id 1.

What's the best method for getting the "release id" from the key? Note if you resort tasks you get:

tasks_for_feature_2[] = 3
tasks_for_feature_2[] = 1
tasks_for_feature_2[] = 2


and I'd like to handle all these with the same method.  I could I guess loop through the entire params array and match keys with a regexp?  But this seems like overkill and I think there are some built in helpers but I'm not sure how to use them.    oh and the tasks and features acts_as_list using position coliumn in the db to set their position.

Thanks!

Re: Sorting Multiple Nested Lists.

If you put square brackets around the number they will be grouped into a hash. For example:

tasks_for_feature[2][] = 3
tasks_for_feature[2][] = 1
tasks_for_feature[2][] = 2

You can loop through this like so:

params[:tasks_for_feature].each do |feature_id, task_ids|
  task_ids.each_with_index do |task_id, index|
    # index+1 is the position
  end
end

Railscasts - Free Ruby on Rails Screencasts