I
I
i_want_to_know_everything2016-09-24 08:21:01
JavaScript
i_want_to_know_everything, 2016-09-24 08:21:01

Sorting nested js object?

there is an object:
how to sort it according to the value of the nested object attribute_1:
Now - so

var data =  [];
    data[0]={ 'attributes' : {'attribute_1' : "60",'attribute_2' : "90"}, 'var' : 'false'};
    data[1]={ 'attributes' : {'attribute_1' : "90",'attribute_2' : "90"}, 'var' : 'false'};
     data[2]={ 'attributes' : {'attribute_1' : "50",'attribute_2' : "90"}, 'var' : 'false'};


It must be so
var data =  [];
    data[0]={ 'attributes' : {'attribute_1' : "50",'attribute_2' : "90"}, 'var' : 'false'};
data[1]={ 'attributes' : {'attribute_1' : "60",'attribute_2' : "90"}, 'var' : 'false'}; 
 data[2]={ 'attributes' : {'attribute_1' : "90",'attribute_2' : "90"}, 'var' : 'false'};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2016-09-24
@i_want_to_know_everything

var items = [
  { name: 'Edward', value: 21 },
  { name: 'Sharpe', value: 37 },
  { name: 'And', value: 45 },
  { name: 'The', value: -12 },
  { name: 'Magnetic' },
  { name: 'Zeros', value: 37 }
];
items.sort(function (a, b) {
  if (a.name > b.name) {
    return 1;
  }
  if (a.name < b.name) {
    return -1;
  }
  // a должно быть равным b
  return 0;
});

An example of sorting an array of objects
Link to documentation:
for reading

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question