Tuesday, January 22, 2013

Ascending And Descending Order In XML


<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
  xmlns:s="library://ns.adobe.com/flex/spark"
  xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Declarations>
<fx:XML id="xmlList" xmlns="">
<datas>
<data>
<xValue>Sal/Payroll</xValue>
<value0>5</value0>
</data>
<data>
<xValue>Discounts</xValue>
<value0>10</value0>
</data>
<data>
<xValue>Depreciation</xValue>
<value0>7</value0>
</data>
<data>
<xValue>Facilities</xValue>
<value0>1</value0>
</data>
<data>
<xValue>Technology</xValue>
<value0>3</value0>
</data>
<data>
<xValue>School Expense</xValue>
<value0>8</value0>
</data>
<data>
<xValue>Debt</xValue>
<value0>4</value0>
</data>
<data>
<xValue>Cap Exp</xValue>
<value0>9</value0>
</data>
<data>
<xValue>Benefits</xValue>
<value0>2</value0>
</data>
<data>
<xValue>Program</xValue>
<value0>6</value0>
</data>
</datas>
</fx:XML>
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.Sort;
import mx.collections.SortField;
import mx.collections.XMLListCollection;
import mx.controls.Alert;
public function sort():void
{
var z:XMLListCollection = new XMLListCollection(xmlList.data as XMLList);
var mySort:Sort = new Sort();
mySort.fields = [new SortField('value0',true,true,true)];
z.sort = mySort;
z.refresh();
txt2.text=z.toXMLString();
}
public function sort2():void
{
var z:XMLListCollection = new XMLListCollection(xmlList.data as XMLList);
var mySort:Sort = new Sort();
mySort.fields = [new SortField('value0',true,false,true)];
z.sort = mySort;
z.refresh();
txt2.text=z.toXMLString();
}
]]>
</fx:Script>
<s:Button label="Ascending Order" click="sort()" x="540" y="8"/>
<s:Button label="Descending Order" click="sort2()" x="658" y="8"/>
<s:TextArea id="txt1" text="{xmlList.data}" x="20" y="32" width="457" height="585" editable="false" fontWeight="bold"/>
<s:TextArea id="txt2" x="540" y="32" width="457" height="585" editable="false" fontWeight="bold"/>
<s:Label x="20" y="12" text="XML-Check The Value" fontWeight="normal" fontSize="16" textDecoration="underline" color="#C91010" textAlign="center"/>
</s:Application>