$twig->parsefile( "file.xml"); # build the twig my $root= $twig->root; # get the root of the twig (stats) my @players= $root->children; # get the player list # sort it on the text of the field my @sorted= sort { $b->first_child( $field)->text <=> $a->first_child( $field)->text } @players; print ''; # print the XML declaration print ''; print ''; # then the root element start tag foreach my $player (@sorted) # the sorted list { $player->print; # print the xml content of the element print "\n"; } print "\n"; # close the document