3 Replies Latest reply on Mar 2, 2010 6:16 AM by Claudino Bur

    Sort Nested Object - 2 properties

    Drew_CA

      Hello,

       

      I am trying to sort based on a few properties of an object. The only issue i am having is the properties are within a nested object.

      For example.

       

      I would like to stort the following object by StreetNumber and StreetName, but these properties are inside 'locations_attributes' array.

       

      {
          "commute": {
              "minutes": 0,
              "startTime": "Wed May 06 22:14:12 EDT 2009",
              "locations_attributes": [
                  {
                      "StreetNumber": "12",
                     "StreetName": "Main"
                      "StreetType": "St"   
                  },
                  {
                      "StreetNumber ": "17",
                     "StreetName": "Morning Side ",
                      "StreetType": "Dr"
                     
                  },
                  {
                      "StreetNumber ": "26",
                      "StreetName": "Blake",
                      "StreetType": "St"               
                  }

       

              ]
          }
      }

       

      Can this be done ?

       

      Drew

        • 1. Re: Sort Nested Object - 2 properties
          Claudino Bur Level 2

          Hello,

           

          Something similar is implemented in dataGridColumn, more exactly it contains complexColumnSortCompare(ob1,obj2) method which returns an int representing the comparison. Check also the method deriveComplexColumnData's code.

           

          What are you trying to sort, what kind of list is it?

           

          Claude Bur.

          • 2. Re: Sort Nested Object - 2 properties
            Drew_CA Level 1

            I get a JSON result from a REST service and I have a custom component that I populate.

            I am not usin a datagrid, but a extended version of the List component.

             

            I am just trying to sort the returned result sorted by street number and street name.

             

            I was using a compareFunction in a sort() method, but only found samples for sorting off of one field.. not 2.

             

            Here is  is a sample of what I was using for 1 field.

            private function sortByAttribute(a:Object, b:Object):Object
                    {
                        var x:String = a.attributes[this._activePanel.SortField].toLowerCase();
                        var y:String = b.attributes[this._activePanel.SortField].toLowerCase();
                        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
                    }

            • 3. Re: Sort Nested Object - 2 properties
              Claudino Bur Level 2

              Yes I understand, your sortFunction would need to look like:

               

               

              if (obj1.street_number > obj2.stree_number)
                   return +1;
              else if (obj1.street_number < obj2.stree_number)
                    return -1;
              else if (obj1.street_number == obj2.stree_number) {
                   if (obj1.street_name > obj2.street_name)
                        return +1;
                   else
                        return -1;
              }