Nokogiri: sort array of ids according to order in HTML document

I have an unsorted array containing the following IDs:

@un_array = ['bar', 'para-3', 'para-2', 'para-7']

      

Is there a sane way to use Nokogiri (or plain Javascript) to sort the array according to the order of the IDs in the example HTML document below?

require 'rubygems'
require 'nokogiri'

value = Nokogiri::HTML.parse(<<-HTML_END)
  "<html>
    <head>
    </head>
    <body>
        <p id='para-1'>A</p>
        <div id='foo'>
            <p id='para-2'>B</p>
        <p id='para-3'>C</p>
            <div id='bar'>
                <p id='para-4'>D</p>
                <p id='para-5'>E</p>
                <p id='para-6'>F</p>
        </div>
         <p id='para-7'>G</p>
        </div>
        <p id='para-8'>H</p>
    </body>
    </html>"
HTML_END

      

In this case, the resulting sorted array should be:

['para-2', 'para-3', 'bar', 'para-7']

      

0


a source to share


3 answers


This is a colleague's solution and I came up with:

parent = value.css('body').first
indexes = []
parent.children.each do |child|
  indexes << child['id']
end

puts @un_array.sort! { |x,y| indexes.index(x) <=> indexes.index(y) }

      



First, I extract all the IDs of the HTML document into an array, than I sort @un_array

according to the previously created array of IDs.

0


a source


I don't know what Nokogiri is, but if you have your HTML as a String it would be possible to get the regex matching order like:

var str = '<html>...</html>'; // the HTML code to check
var ids = ['bar', 'para-3', 'para-2', 'para-7']; // the array with all IDs to check
var reg = new RegExp('(?:id=[\'"])('+ids.join('|')+')(?:[\'"])','g') // the regexp
var result = [], tmp; // array holding the result and a temporary variable
while((tmp = reg.exec(str))!==null)result.push(tmp[1]); // matching the IDs
console.log(result); // ['para-2', 'para-3', 'bar', 'para-7']

      



using this code, you should be careful with identifiers containing regexp metacharacters. They should be avoided at first.

+1


a source


Here's one way to do it in Nokogiri - there may be others that are more efficient, since it ends up going all over the DOM.

require 'set'

#Using a set here to make lookup O(1), because we don't care about the initial order
id_set = ['bar', 'para-3', 'para-2', 'para-7'].to_set
sorted = []

value.root.traverse do |node|
  node_id = node['id']
  sorted << node_id if node_id && id_set.delete?(node_id)
end
# sorted is now ['para-2', 'para-3', 'bar', 'para-7']

      

EDIT: Here's a one-liner that gets the same results, but I haven't benchmarked to see which is faster.

ids = ['bar', 'para-3', 'para-2', 'para-7']
value.xpath("//*[@id]").collect {|node| node['id']} & ids

      

0


a source







All Articles