Good day to all. I'm new to js, ​​so I often run into problems writing scripts. In short, I have a json array of objects:

var MyJson = '[ '+ '{ "id":"Id","name":"Name","price": "Price","quantity":"Quantity"}, '+ '{ "id":"123","name":"iPhone XS","price": "1600","quantity":"10"}, '+ '{ "id":"344","name":"Samsung Galaxy S7","price": "550","quantity":"7"}, '+ '{ "id":"266","name":"Macbook","price": "900","quantity":"7"},'+ '{ "id":"478","name":"Asus","price": "400","quantity":"8"}, '+ '{ "id":"569","name":"Acer","price": "300","quantity":"4"}, '+ '{ "id":"788","name":"TP-LINK","price": "100","quantity":"10"}, '+ '{ "id":"124","name":"iPhone SE","price": "350","quantity":"11"}, '+ '{ "id":"345","name":"Samsung Galaxy Note Boom","price": "690","quantity":"8"}, '+ '{ "id":"267","name":"Surface Boom","price": "690","quantity":"8"}'+ ']'; 

There was a need to sort the objects, say by id. Can you please tell someone how to junk it up with Javascript.

  • Now you do not have objects but a string =) objects will be after the execution of this line: var Myobj = JSON.parse(MyJson); describe the function to sort, and use the sort method - Vladimir Klykov
  • Something with the sorting function is not good; I found function dynamicSort (property) {var sortOrder = 1; if (property [0] === "-") {sortOrder = -1; property = property.substr (1); } return function (a, b) {var result = (a [property] <b [property])? -1: (a [property]> b [property])? ten; return result * sortOrder; }} But something is not working. And in this moment I call: document.getElementById ('thId3'). AddEventListener ('click', function () {Myobj.sort (dynamicSort ('id'));}); - Okunev Pasha
  • I have no idea what you were looking for, but (I don’t know much about JS) I found a completely different example and judging by the code (by the way it is implemented in other PLs) - similar to the truth, see the answer in a minute .... - Vladimir Klykov

2 answers 2

 var MyJson = '[ '+ '{ "id":"Id","name":"Name","price": "Price","quantity":"Quantity"}, '+ '{ "id":"123","name":"iPhone XS","price": "1600","quantity":"10"}, '+ '{ "id":"344","name":"Samsung Galaxy S7","price": "550","quantity":"7"}, '+ '{ "id":"266","name":"Macbook","price": "900","quantity":"7"},'+ '{ "id":"478","name":"Asus","price": "400","quantity":"8"}, '+ '{ "id":"569","name":"Acer","price": "300","quantity":"4"}, '+ '{ "id":"788","name":"TP-LINK","price": "100","quantity":"10"}, '+ '{ "id":"124","name":"iPhone SE","price": "350","quantity":"11"}, '+ '{ "id":"345","name":"Samsung Galaxy Note Boom","price": "690","quantity":"8"}, '+ '{ "id":"267","name":"Surface Boom","price": "690","quantity":"8"}'+ ']'; var data = JSON.parse(MyJson).sort(function(a, b) { return a.id - b.id; }); console.log(data); 

And how to be with name? The same sort doesn't work anymore.

 var MyJson = '[ '+ '{ "id":"Id","name":"Name","price": "Price","quantity":"Quantity"}, '+ '{ "id":"123","name":"iPhone XS","price": "1600","quantity":"10"}, '+ '{ "id":"344","name":"Samsung Galaxy S7","price": "550","quantity":"7"}, '+ '{ "id":"266","name":"Macbook","price": "900","quantity":"7"},'+ '{ "id":"478","name":"Asus","price": "400","quantity":"8"}, '+ '{ "id":"569","name":"Acer","price": "300","quantity":"4"}, '+ '{ "id":"788","name":"TP-LINK","price": "100","quantity":"10"}, '+ '{ "id":"124","name":"iPhone SE","price": "350","quantity":"11"}, '+ '{ "id":"345","name":"Samsung Galaxy Note Boom","price": "690","quantity":"8"}, '+ '{ "id":"267","name":"Surface Boom","price": "690","quantity":"8"}'+ ']'; var data = JSON.parse(MyJson).sort(function(a, b) { return ((a.name === b.name) ? 0 : ((a.name > b.name) ? 1 : -1)); }); console.log(data); 

  • Ahead of me =) and even the inline function =)) - Vladimir Klykov
  • @VladimirKlykov, ahah, fast train :) - Let's say Pie
  • =) JS is not my language at all, I write it with a dictionary =) - Vladimir Klykov
  • @ Vladimir Klykov, a beginner? - Let's say Pie
  • in programming or in js? in js - I don’t strive, I just write as needed =) but generally delphi is 10+ years old, a couple of years of php but for myself purely, a couple of years in C for microcontrollers but also purely for myself =) - Vladimir Klykov

Something like this should work:

 function compareId(Obj1, Obj2) { return Obj1.id - Obj2.id; } .... var MyObj = JSON.parse(MyJson); MyObj.sort(compareId);