0

I am facing problem to sort an multiple object with nested property(Price)

{  
"EI":[  
  {  
     "offerid":"1S-EI-715",
     "Price":"30.00"
  },
  {  
     "offerid":"1S-IB-518",
     "Price":"20.00"
  }
  ],
"IB":[  
  {  
     "offerid":"1S-IB-518",
     "Price":"10.00"
  },
  {  
     "offerid":"1S-IB-518",
     "Price":"40.00"

  }
]}

I need to sort the price like below. Is it possible to sort like that. Kindly give some solution for this. Thanks in advance

{  
"IB":[  
  {  
     "offerid":"1S-IB-518",
     "Price":"10.00"
  },
  {  
     "offerid":"1S-IB-518",
     "Price":"40.00"

  }
  ]
 "EI":[ 
   {  
     "offerid":"1S-IB-518",
     "Price":"20.00"
  }
  {  
     "offerid":"1S-EI-715",
     "Price":"30.00"
  },
   ]}

2 Answers 2

1

Objects can't be sorted. I suggest you map the data to single array that would like more like:

[
    {
        "type" : "EI",
        "offerid" : "1S-EI-715",
        "Price" : "30.00"
    },       
    {
        "type" : "IB",
        "offerid" : "1S-IB-518",
        "Price" : "40.00"    
    }
]

var data = {
  "EI": [{
    "offerid": "1S-EI-715",
    "Price": "30.00"
  }, {
    "offerid": "1S-IB-518",
    "Price": "20.00"
  }],
  "IB": [{
    "offerid": "1S-IB-518",
    "Price": "10.00"
  }, {
    "offerid": "1S-IB-518",
    "Price": "40.00"
  }]
}

var results = Object.keys(data).reduce(function(arr, key) {
  var newItems = data[key].map(function(item) {
    item.type = key;
    return item
  })
  return arr.concat(newItems)
}, []).sort(function(a, b) {
  return +a.Price - +b.Price
})

console.log(results)

Sign up to request clarification or add additional context in comments.

Comments

0

See possible solution using ng-repeat on view level. Showing desc and asc approach.

VIEW

<div id="app" ng-app="myApp" ng-controller="myCtrl">
  <b>Order ASC</b>
  <div ng-repeat="(name,data) in myarr">
    {{name}}
    <br/>
    <div ng-repeat="(itemindex,itemdata) in data | orderBy:'Price'">
      {{itemdata}}
    </div>
  </div>
  <br/>
  <b>Order DESC</b>
  <div ng-repeat="(name,data) in myarr">
    {{name}}
    <br/>
    <div ng-repeat="(itemindex,itemdata) in data | orderBy:'-Price'">
      {{itemdata}}
    </div>
  </div>
</div>

MODULE

angular.module('myApp', []).controller('myCtrl', function($scope) {

  $scope.myarr = {
    "EI": [{
      "offerid": "1S-EI-715",
      "Price": "30.00"
    }, {
      "offerid": "1S-IB-518",
      "Price": "20.00"
    }],
    "IB": [{
      "offerid": "1S-IB-518",
      "Price": "10.00"
    }, {
      "offerid": "1S-IB-518",
      "Price": "40.00"
    }]
  };

});

RESULT

Order ASC
EI 
{"offerid":"1S-IB-518","Price":"20.00"}
{"offerid":"1S-EI-715","Price":"30.00"}
IB 
{"offerid":"1S-IB-518","Price":"10.00"}
{"offerid":"1S-IB-518","Price":"40.00"}

Order DESC
EI 
{"offerid":"1S-EI-715","Price":"30.00"}
{"offerid":"1S-IB-518","Price":"20.00"}
IB 
{"offerid":"1S-IB-518","Price":"40.00"}
{"offerid":"1S-IB-518","Price":"10.00"}

See Fiddle

1 Comment

While order by ASC, parent IB should come first because inside IB I am having 10 then 40. Parent also have to be sorted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.