2 Replies Latest reply on Jan 29, 2010 11:50 AM by NonTakenUserName

    Sorting XMLListCollection by nested elements

    NonTakenUserName

      I've been sorting XML files using an XMLListCollection when I have an XML with the following structure:

       

      <products>
           <product id="1">
                <field1>value</field1>
      
                <field2>value</field2>
                <field3>value</field3>
      
           </product>
      
           <product id="2">
                <field1>value</field1>
                <field2>value</field2>
                <field3>value</field3>
           </product>
      
      </products>
      

       

      by using an XMLListCollection as follows:

       

      var mySort:sort = new Sort();
      var xlcProducts:XMLListCollection = new XMLListCollection();
      
      mySort.fields = [new SortField("field3"), new SortField("field2")]
      
      
      xlcProducts.source = dataSet.elements("product");
      xlcProducts.sort = mySort;
      xlcProducts.refresh();
      

       

      Now I'm dealing with a more complicated XML that follows this structure:

       

       

      <products>
           <product>
                <productID>607</productID>
                <productName>ADS1000</productName>
                <features>
                     <featureData>
                          <featureName>feature1</featureName>
                          <featureValue>value1</featureValue>
                     </featureData>
                     <featureData>
                          <featureName>feature2</featureName>
                          <featureValue>value2</featureValue>
                     </featureData>
                     <featureData>
                          <featureName>feature3</featureName>
                          <featureValue>value3</featureValue>
                     </featureData>
                </features>
           </product>
           <product>
                <productID>607</productID>
                <productName>ADS1000</productName>
                <features>
                     <featureData>
                          <featureName>feature1</featureName>
                          <featureValue>value1</featureValue>
                     </featureData>
      
                     <featureData>
                          <featureName>feature2</featureName>
                          <featureValue>value2</featureValue>
                     </featureData>
      
                     <featureData>
                          <featureName>feature3</featureName>
                          <featureValue>value3</featureValue>
                     </featureData>
                </features>
           </product>
      </products>
      

       

       

       

       

      My problem here is that the same approach doesn't seem to work, when I create the mySort object to assign to the XMLListCollection I can't seen to be able to point it to a field that is not on the top level of the XML's children. Here, I need to sort the XML by featureValue for two different features (which will change at different points in time)

       

      I've tried to do stuff like this to no avail:

       

      mySort.fields = [new SortField("features.featureData.(featureName == \"feature3\").featureValue"), new SortField("features.featureData.(featureName == \"feature2\").featureValue")]

       

       

      I'm assuming I can't have XML queries in the SortField's name, but nothing else I've tried has worked.