Sorting in JavaScript multidimensional arrays in one of the settings?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
47 views
Hi,
I don't know how to sort multidimensional arrays.
there is an array:
var item_unitaz = [ {'item_id':1, 'article':'DE23421312', 'name':'Hello','type':2}, {'item_id':2, 'article':'DE12421442', 'name':'Until','type':32}, {'item_id':3, 'article':'DE78677833', 'name':'Sparrow','type':68}, {'item_id':4, 'article':'DE23442235', 'name':'Rooster','type':45}];

Need to sort this array by value Type in the correct order (2, 32, 45, 68)

All I thought of is:
for (var i = 0; i < item_unitaz.length; i++) { console.log(item_unitaz[i].type.sort(function(a,b){return a - b})); }

Of course that didn't work...
Please help!
by | 47 views

6 Answers

0 like 0 dislike
var item_unitaz = [ {'item_id':1, 'article':'DE23421312', 'name':'Hello','type':2}, {'item_id':2, 'article':'DE12421442', 'name':'Until','type':32}, {'item_id':3, 'article':'DE78677833', 'name':'Sparrow','type':68}, {'item_id':4, 'article':'DE23442235', 'name':'Rooster','type':45}]; compareUnitaz function(a, b) { return a.type - b.type; } item_unitaz.sort(compareUnitaz)
by
0 like 0 dislike
var item_unitaz = [ {'item_id':1, 'article':'DE23421312', 'name':'Hello','type':2}, {'item_id':2, 'article':'DE12421442', 'name':'Until','type':32}, {'item_id':3, 'article':'DE78677833', 'name':'Sparrow','type':68}, {'item_id':4, 'article':'DE23442235', 'name':'Rooster','type':45}]; compareUnitaz function(a, b) { if (a.type > b.type) return 1; if (a.type < b.type) return -1; return 0; } item_unitaz.sort(compareUnitaz);
by
0 like 0 dislike
by
0 like 0 dislike
As the saying goes, look in the textbook.
Objects can be sorted by the value of one of its properties.

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 should equal b return 0; });
by
0 like 0 dislike
const sortArray = (array, pole) => { return array.sort((a,b)=> {return (a[pole] > b[pole]) ? 1 : ((b[pole] > a[pole]) ? -1 : 0);} ); } console.log(sortArray(item_unitaz,"type"))
by
0 like 0 dislike
'use strict'; var table = [ { ll: 5 ss: 10 }, { ll: 55, ss: 10 , aa:25}, { ll: 100, ss: 10, aa:{a:1,b:2} }, { ll: 70, ss: 10 } ]; table.sort(function (a, b) { return a.ll < b.ll });

Read more
by

Related questions

0 like 0 dislike
3 answers
0 like 0 dislike
3 answers
0 like 0 dislike
3 answers
asked Jul 10, 2019 by dmitrylee
0 like 0 dislike
3 answers
110,608 questions
257,186 answers
0 comments
35,533 users