Murcia has asked for the wisdom of the Perl Monks concerning the following question:

Hi, confreres, I have a problem with XML::DOM.

In my XML file I have a tag named <function>

eg:

<function name='a' start='1'>
<function name='b' start='2'>
<function name='c' start='3'>
<function name='a' start='4'>
<function name='b' start='5'>
the code comes here, it prints unorded!
my $nodes = $data->getElementsByTagName('function'); my $n = $nodes->getLength; for (my $i = 0; $i < $n; $i++) { my $node = $nodes->item ($i); my $start = $node->getAttributeNode ("start"); print $start->getValue . "\n"; }
is it possible to sort, so that all with name a, then b, then c print out?

thanks

Murcia

Replies are listed 'Best First'.
Re: XML::DOM sort
by chromatic (Archbishop) on Jun 04, 2003 at 18:16 UTC

    This is why I hate the (excessively verbose) DOM:

    my @sorted = map { $_->[1] } sort { $a->[0] cmp $b->[0] } map { my $item = $nodes->item( $_ ); [ $item->getAttributeNode( 'name' )->getValue(), $item ] } 0 .. $nodes->getLength() ;
      thanks that works nearly $nodes->getLength() -1 !! ;-) Murcia