Method: Rubyvis::Dom::Node#sort

Defined in:
lib/rubyvis/dom.rb

#sort(f_a = nil, &f) ⇒ Object

Sorts child nodes of this node, and all descendent nodes recursively, using the specified comparator function f. The comparator function is passed two nodes to compare.

<p>Note: during the sort operation, the comparator function should not rely on the tree being well-formed; the values of previousSibling and nextSibling for the nodes being compared are not defined during the sort operation.

Parameters:

  • f (function)

    a comparator function.



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/rubyvis/dom.rb', line 281

def sort(f_a=nil,&f)
  f=f_a unless f_a.nil?
  raise "Should pass a Proc" if f.nil?
  if @first_child
    @child_nodes.sort!(&f)
    _p=@first_child = child_nodes[0]
    _p.previous_sibling=nil
    (1...@child_nodes.size).each {|i|
      _p.sort(&f)
      c=@child_nodes[i]
      c.previous_sibling=_p
      _p=_p.next_sibling=c
    }
    @last_child=_p
    _p.next_sibling=nil
    _p.sort(f)
  end
  self
end