# File lib/ancestry/class_methods.rb, line 67 def sort_by_ancestry(nodes, &block) arranged = nodes if nodes.is_a?(Hash) unless arranged presorted_nodes = nodes.sort do |a, b| a_cestry, b_cestry = a.ancestry || '0', b.ancestry || '0' if block_given? && a_cestry == b_cestry yield a, b else a_cestry <=> b_cestry end end arranged = arrange_nodes(presorted_nodes) end arranged.inject([]) do |sorted_nodes, pair| node, children = pair sorted_nodes << node sorted_nodes += sort_by_ancestry(children, &block) unless children.blank? sorted_nodes end end