Here is an example app:

Tracy

 

<?xml version="1.0" encoding="utf-8"?>

   <mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"

                                    initialize="sortSetDP()">

      <mx:Script>

<![CDATA[

                                                 

   /*************************************************************************************

               

   */      

            private function sortSetDP(sMode:String):Void

            {

                        var aTemp:Array = aApplesMaster.concat();

                        switch (sMode)

                        {

                                    case "top":

                                                aTemp.sort(cfAppleSales,Array.DESCENDING );

                                                dgAppleSales.dataProvider = aTemp.splice(0,3);

                                                dgAppleSales.rowCount = aTemp.length;

                                                break;

                                    case "bottom":

                                                aTemp.sort(cfAppleSales );

                                                dgAppleSales.dataProvider = aTemp.splice(0,3);

                                                dgAppleSales.rowCount = aTemp.length;                        

                                                break;

                                    default:

                                                dgAppleSales.dataProvider = aApplesMaster;

                                                dgAppleSales.rowCount = aApplesMaster.length;

                                                break;                                                                                      

                        }

 

            }//sortSetDP

                                                 

   /*************************************************************************************

      Compare function.  Receives  two array elements as arguments

   */      

            private function cfAppleSales(oElementA:Object,oElementB:Object):Number

            {

                        var nQtyA:Number = oElementA.qty;       //get the A element quantity value

                        var nQtyB:Number = oElementB.qty;       //get the B element quantity value

                        if (nQtyA > nQtyB)  {                                                      //compare the quantity values, and return the appropriate value

                                    return 1;

                        }

                        else if (nQtyB > nQtyA)  {

                                    return -1;

                        }

                        return 0;

            }//cfAppleSales

           

]]>

</mx:Script>

      <mx:Array id="aApplesMaster">

         <mx:Object>

            <label>Shopper1</label>

            <qty>5</qty>

         </mx:Object>

         <mx:Object>

            <label>Shopper2</label>

            <qty>7</qty>

         </mx:Object>

         <mx:Object>

            <label>Shopper3</label>

            <qty>0</qty>

         </mx:Object>

         <mx:Object>

            <label>Shopper4</label>

            <qty>4</qty>

         </mx:Object>

         <mx:Object>

            <label>Shopper5</label>

            <qty>1</qty>

         </mx:Object>

         <mx:Object>

            <label>Shopper6</label>

            <qty>0</qty>

         </mx:Object>

         <mx:Object>

            <label>Shopper7</label>

            <qty>0</qty>

         </mx:Object>

         <mx:Object>

            <label>Shopper8</label>

            <qty>0</qty>

         </mx:Object>

      </mx:Array>

 

            <mx:HBox>

                        <mx:VBox>

                                    <mx:Button label="Show Top Three Customers" height="30" click="sortSetDP('top')"/>

                                    <mx:Button label="Show Bottom Three Customers" height="30" click="sortSetDP('bottom')"/>

                                    <mx:Button label="Show All Customers" height="30" click="sortSetDP('all')"/>

                        </mx:VBox>

                        <mx:DataGrid id="dgAppleSales"  >

                                    <mx:columns>

                                                <mx:Array>

                                                            <mx:DataGridColumn headerText="Shopper" columnName="label" />

                                                            <mx:DataGridColumn headerText="Apples Purchased" columnName="qty" />

                                                </mx:Array>

                                    </mx:columns>

                        </mx:DataGrid>

            </mx:HBox>

   </mx:Application>

 


From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED]
Sent: Wednesday, May 18, 2005 9:29 PM
To: [email protected]
Subject: Re: [flexcoders] Is there an easy way to do this

 

I tried what you said and what I thought was the Array.sort command but I can not get it to work. What I am trying to do in a nutshell is sort the array by showing the top three numbers in order on either a button or a label next to there respective Shoppers. I looked online for info on the Array.sort() function but I could not see a good example of it in use?

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml">
<mx:Array id="apples">
  <mx:Object>
   <label>Shopper1</label>
   <Number>5</Number>
  </mx:Object>
  <mx:Object>
   <label>Shopper2</label>
   <Number>7</Number>
  </mx:Object>
<mx:Object>
   <label>Shopper3</label>
   <Number>0</Number>
  </mx:Object>
<mx:Object>
   <label>Shopper4</label>
   <Number>4</Number>
  </mx:Object>
  <mx:Object>
   <label>Shopper5</label>
   <Number>1</Number>
  </mx:Object>
<mx:Object>
   <label>Shopper6</label>
   <Number>0</Number>
  </mx:Object>

 

<mx:Object>
   <label>Shopper7</label>
   <Number>0</Number>
  </mx:Object>
<mx:Object>
   <label>Shopper8</label>
   <Number>0</Number>
  </mx:Object>

 


 </mx:Array>
 <mx:Script>
  <![CDATA[
   public function getorder():Void
   {
    Array.apples.sort(label,Number)
firstplaceshopper.label=apples[0].label
mostapples.label=apples[0].Number
secondplaceshopper.label=apples[1].label
secondmostapples.label=apples[1].Number
thirdplaceshopper.label=apples[2].label
thirdmostapples.label=apples[2].Number
   }
  ]]>
 </mx:Script>
 
 
  
 <mx:HBox id="whohasthemost" visible="true">

 


  <mx:Button label="firstshopper" />
<mx:Button width="100" id="firstplaceshopper" />
<mx:Button label="Number of apples" />
<mx:Button width="30" id="mostapples"/>

 

<mx:Button label="secondshopper" />
<mx:Button width="100" id="secondplaceshopper" />
<mx:Button label="Number of apples" />
<mx:Button width="30" id="secondmostapples"/>
<mx:Button label="thirdshopper" />
<mx:Button width="100" id="thirdplaceshopper" />
<mx:Button label="Number of apples" />
<mx:Button width="30" id="thirdmostapples"/>

 

<mx:Button label="gettopthree" click="getorder()"/>

 

</mx:HBox>
</mx:Application>

 



Yahoo! Groups Links

Reply via email to