0

I am using angular js to sort json data. everything is working fine but the part where empty or json with null data shows up right at top. Anyone has an example that pushes both empty and null data to bottom .

Example

{itemId: 42608125,
parentItemId: 42608125,
name: "Apple iPod touch 32GB, Assorted Colors",
salePrice: 237.49,
upc: "888462353151"
},
{itemId: 39875894,
parentItemId: 39875894,
name: "Griffin Survivor Extreme-Duty Case for Apple iPod touch 5G, Blue",
msrp: 29.88,
salePrice: 22.64
}

As you can see here one has msrp and other does not . SO when I order by msrp I see the one not having anything set at top.

       <tr ng-repeat="u in ctrl.recommededResults | orderBy:'-productReview.reviewStatistics.averageOverallRating'">
                      <td><span ng-bind="u.itemId"></span></td>
                      <td><span ng-bind="u.name"></span></td>
                      <td><span ng-bind="u.msrp"></span></td>
                      <td><span ng-bind="u.productReview.reviewStatistics.averageOverallRating"></span></td>
                  </tr>

enter image description here

Praveen
  • 101
  • 14
  • It's hard to say without any examples, but I suggest you filter the results to remove the null array items in your collection. – filype Jun 06 '16 at 00:04
  • I still want the items but just at bottom – Praveen Jun 06 '16 at 00:05
  • JavaScript (and by extension, Angular) does not define an order for properties within an object. Angular does allow you to iterate through objects by `(key, value)`, but not key ordering. https://docs.angularjs.org/api/ng/directive/ngRepeat : "•The JavaScript specification does not define the order of keys returned for an object, so Angular relies on the order returned by the browser when running `for key in myObj`. Browsers generally follow the strategy of providing keys in the order in which they were defined, although there are exceptions when keys are deleted and reinstated." – Claies Jun 06 '16 at 00:35
  • essentially, if you are trying to do a sort and getting items in a different order, a custom sort function would be necessary. As others have said, more information about your current sorting method is necessary before a custom method can be suggested. – Claies Jun 06 '16 at 00:38
  • does this help. I am using the default order by that angular provides – Praveen Jun 06 '16 at 00:45

1 Answers1

0

I believe you need to use a custom function to define how to order by.

Without any examples, it's hard to provide the code you need.

See examples in this SO post:

Custom sort function in ng-repeat

Community
  • 1
  • 1
filype
  • 8,034
  • 10
  • 40
  • 66