3 Replies Latest reply on Sep 22, 2010 3:22 PM by saisri2k2

    Sort.findItem question

    Sagar MJ Level 1

      I am trying the following simple code....but am not sure why sort.findItem() does not work. Please help.


      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="sortCollection()">
              <s:ArrayCollection id="collection">
                  <fx:Object name="Martin Foo" age="25" />
                  <fx:Object name="Joe Bar" age="15" />
                  <fx:Object name="Joe Baz" age="23" />
                  import mx.collections.*;
                  import mx.controls.Alert;
                  private var personCollection:ArrayCollection = new ArrayCollection();


                  public function sortCollection():void
                      var obj1:Object = new Object();
                      var obj2:Object = new Object();
                      var obj3:Object = new Object();
                      obj1.name = "Martin Foo";
                      obj1.age = 25;
                      obj2.name = "Joe Far";
                      obj2.age = 15;
                      obj3.name = "Joe Baz";
                      obj3.age = 23;
                  private function sortByAge():void
                      var sort:Sort = new Sort();
                      sort.fields = [new SortField("age", false, false)];
                      personCollection.sort = sort;
                  private function getName(item:Object):String
                      return item.name;
                  private function checkExistence():void
                      var searchObject:Object = new Object();
                      searchObject["name"] = personName.text;
                      searchObject["age"] = Number(personAge.text);
                      var sort: Sort = new Sort();
                      var indexP:int =  sort.findItem(personCollection.source,
                          Alert.show("The Person already exists" + indexP);
                          var person:Object = new Object();
                          person.name = personName.text;
                          person.age = personAge.text;


          <s:DropDownList id="names" dataProvider="{personCollection}" x="344" y="134" width="378" height="22" labelFunction="getName"></s:DropDownList>
          <s:TextInput x="359" y="205" id="personName"/>
          <s:TextInput x="359" y="235" id="personAge"/>
          <s:Label x="309" y="209" text="Name" width="42" height="22" textAlign="center" verticalAlign="middle"/>
          <s:Label x="307" y="239" text="Age" width="42" height="22" textAlign="center" verticalAlign="middle"/>
          <s:Button x="572" y="211" label="Add" id="addButton" click="checkExistence()"/>

        • 1. Re: Sort.findItem question
          saisri2k2 Level 4

          because the Object that you are creating and the object that you are trying to find are different, you have to loop through the collection to find the item.




          for each(var obj:Object in personCollection){

          if( obj["name"] == searchObject["name"]){

          return true;




          • 2. Re: Sort.findItem question
            Sagar MJ Level 1

            I didn't get your reply. Both the objects are same....they are not different. I think that's not the solution...please explain




            • 3. Re: Sort.findItem question
              saisri2k2 Level 4

              The object whch you are trying to search is not a part of the collection. You are trying to compare a brand new object with the already existing objects. If you have debugger installed on your system try to look at the memory location (usually looks like @234Sfsfdfe3 for evry object) of the searchObject and all the objecs in the arrayCollection, none of the objects would match the memory location with the searchObject, Because you are trying to do == on two unequal objects(if you do == then the object ref has to be same for the source and the target).


              P.S: two objects will never be equal if the memory locations are different. You have to manually compare the properties of the two objects to find if the two objects are similar.