Thank you Dear, your coding is fentastic. This solution is working. Even I did my code as below. But my code was specific to elements.Here is my code

#!/usr/bin/perl use strict; use warnings; use XML::Twig; my (@sortedM, @sortedL); my $i=0; my $twig= new XML::Twig(twig_handlers => { 'enhancement' => sub {$_->first_child- +>sort_children_on_att("type");}, 'descriptor' => \&sortSubEltsHandler, }, pretty_print => 'indented', keep_atts_order => 1, ); $twig->parse(\*DATA); $twig->print; $twig->purge; sub sortSubEltsHandler { my( $t,$elt)= @_; my $root= $t->root; my @mainterms = $root->descendants(q{mainterm}); @sortedM = reverse sort { $a->text cmp $b->text } @mainterms; my @links = $root->descendants(q{link}); @sortedL =reverse sort { $a->text cmp $b->text } @links; $elt->cut_children; foreach my $mainterm (@sortedM) { my $new_elt= XML::Twig::Elt->new($mainterm->name => $mainterm->tex +t); $new_elt->paste($elt); } foreach my $link (@sortedL) { my $new_elt= XML::Twig::Elt->new($link->name => $link->text); $new_elt->paste($elt); } }#end of sortSubEltsHandler __DATA__ <enhancement> <descriptorgroup> <descriptors type="MED" controlled="y"> <descriptor> <mainterm weight="a">plague</mainterm> <mainterm>disease</mainterm> <mainterm>pestilence</mainterm> <link>f</link> <link>C</link> <link>M</link> </descriptor> </descriptors> <descriptors type="DRG" controlled="y"> <descriptor> <mainterm>panadol</mainterm> <mainterm weight="a">zorro</mainterm> <mainterm>xenon</mainterm> <mainterm weight="a">aspirin</mainterm> <link>b</link> <link>P</link> <link>z</link> </descriptor> </descriptors> </descriptorgroup> </enhancement>

In reply to Re^4: twig inbuilt methods to sort whole xml tree by bharathinc
in thread twig inbuilt methods to sort whole xml tree by bharathinc

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.