From: "Muhammet S. AYDIN" Date: 2011-12-22T04:56:17+09:00 Subject: sort optimization questions --f46d044288908549cc04b49f93a0 Content-Type: text/plain; charset=ISO-8859-1 hello everyone, i have a model class Page in my rails application which is defined like this: class Page < ActiveRecord::Base attr_accessor :children # relationships here.. end and a NodeList class to sort this pages based on their parent_id attributes: http://pastie.org/private/rbq05vzkc7wgyan8pcdrw here is a simple example of how i'm using them: node_list = NodeList.new(Page.all()) @pages_list = node_list.get_nodes() i also have a helper method to print the tree: def print_children(nodes) retval = "" return retval end currently it is giving me what i need: - Parent 1 -- Child 1 --- Sub Child 1 --- Sub Child 2 -- Child 2 -- Child 3 - Parent 2 -- Child 4 -- Child 5 --- Sub Child 3 so, here are my two questions: - how can i make this better performing and rubyish? - as this is looping through every element of @page_list, after i'm done with the element, i would like to remove it with @page_list.delete(obj) in get_children method after the line "tmp << obj" however this is causing me lose data so instead of the above structure i get something like this: - Parent 1 -- Child 1 --- Sub Child 1 -- Child 2 - Parent 2 -- Child 4 why is this happening? thanks in advance. Muhammet S. AYDIN http://mengu.net http://compector.com --f46d044288908549cc04b49f93a0--