I would like to sort based on an attribute. I am working on an automatic 
derived object publishing system using ClearCase for sharing do's across 
different types of automated build systems of which some may not be using 
ClearCase. Since my perl program is over a 1000 lines, I thought an 
existing example from the xmltwig site would help. 

I added an attribute called vob.  Note: not all names have this attribute
because it also has to publish to a simple directory structure.

<stats><player vob="d" ><name>Houston, Allan</name><g>69</g></player>
<player vob="z" ><name>Sprewell, Latrell</name><g>69</g></player>
<player vob="a" ><name>Ewing, Patrick</name><g>49</g></player>
<player vob="none" ><name >Johnson, Larry</name><g>57</g></player>
<player vob="g" ><name>Camby, Marcus</name><g>48</g></player>
<player vob="d" ><name>Thomas, Kurt</name><g>67</g><ppg>7.9</ppg></player>
<player vob="none" ><name >Ward, Charlie</name><g>59</g><ppg>7.5</ppg></player>
<player vob="none" ><name>Wallace, John</name><g>55</g><ppg>6.6</ppg></player>
<player><name>Childs, Chris</name><g>61</g><ppg>5.3</ppg></player>
<player><name>Duncan, Tim</name><g>66</g><ppg>23.1</ppg></player>
<player><name>Robinson, David</name><g>68</g><ppg>17.1</ppg></player>

</stats>

I would like them to be ordered like this:
No attribute or vob=none
--------------
Childs, Chris
Duncan, Tim
Robinson, David
Johnson, Larry    ....none
Ward, Charlie     ....none
Wallace, John     ....none

a,d,g,none,z
--------------
Ewing, Patrick    ....a
Houston, Allan    ....d
Thomas, Kurt      ....d
Camby, Marcus     ....g
Sprewell, Latrell ....z


Here is the example for sorting.
In the example below, the sort command is numerically sorting on first child.

My current plan is as follows:
Run through the entire players array collecting the values for the vob attribute,
and then sort and make unique my attribute values. While looping through the 
unique vob attribute values, loop through the players array testing
to see if each player matched the attribute.

I was hoping for something more elegant.

Any suggestions would be appreciated.
Thanks,

Patrick

#########################################################################
#                                                                       #
#  This first example shows how to create a twig, parse a file into it  #
#  get the root of the document, its children, access a specific child  #
#  and get the text of an element                                       #
#                                                                       #
#########################################################################

use strict;
use XML::Twig;

my $field= $ARGV[0] || 'name';
my $twig= new XML::Twig;

$twig->parsefile( "nba.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 '<?xml version="1.0"?>';   # print the XML declaration
print '<!DOCTYPE stats SYSTEM "stats.dtd" []>';
print '<stats>';                 # 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 "</stats>\n";              # close the document

In reply to XML::Twig -- sorting by attribute by Novitiatus

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.