在Javascript中sorting一个JSON对象

我一直在寻找一个像这样sorting一个JSON对象

{"results": [ { "layerId": 5, "layerName": "Pharmaceutical Entities", "attributes": { "OBJECTID": "35", "FACILITYTYPE": "Pharmacy", "FACILITYSUBTYPE": "24 Hr Pharmacy", "COMMERCIALNAME_E": "SADD MAARAB PHARMACY", }, "geometryType": "esriGeometryPoint", }, { "layerId": 5, "layerName": "Pharmaceutical Entities", "attributes": { "OBJECTID": "1", "FACILITYTYPE": "Pharmacy", "FACILITYSUBTYPE": "24 Hr Pharmacy", "COMMERCIALNAME_E": "GAYATHY HOSPITAL PHARMACY", }, "geometryType": "esriGeometryPoint", }, { "layerId": 5, "layerName": "Pharmaceutical Entities", "attributes": { "OBJECTID": "255", "FACILITYTYPE": "Pharmacy", "FACILITYSUBTYPE": "24 Hr Pharmacy", "COMMERCIALNAME_E": "AL DEWAN PHARMACY", }, "geometryType": "esriGeometryPoint", } ]} 

按字母顺序按“COMMERCIALNAME_E”取值

 {"results": [ { "layerId": 5, "layerName": "Pharmaceutical Entities", "attributes": { "OBJECTID": "255", "FACILITYTYPE": "Pharmacy", "FACILITYSUBTYPE": "24 Hr Pharmacy", "COMMERCIALNAME_E": "AL DEWAN PHARMACY", }, "geometryType": "esriGeometryPoint", }, { "layerId": 5, "layerName": "Pharmaceutical Entities", "attributes": { "OBJECTID": "1", "FACILITYTYPE": "Pharmacy", "FACILITYSUBTYPE": "24 Hr Pharmacy", "COMMERCIALNAME_E": "GAYATHY HOSPITAL PHARMACY", }, "geometryType": "esriGeometryPoint", }, { "layerId": 5, "layerName": "Pharmaceutical Entities", "attributes": { "OBJECTID": "35", "FACILITYTYPE": "Pharmacy", "FACILITYSUBTYPE": "24 Hr Pharmacy", "COMMERCIALNAME_E": "SADD MAARAB PHARMACY", }, "geometryType": "esriGeometryPoint", } ]} 

我找不到任何代码可以做到这一点。 任何人都可以给我一些帮助吗?

 function sortJsonArrayByProperty(objArray, prop, direction){ if (arguments.length<2) throw new Error("sortJsonArrayByProp requires 2 arguments"); var direct = arguments.length>2 ? arguments[2] : 1; //Default to ascending if (objArray && objArray.constructor===Array){ var propPath = (prop.constructor===Array) ? prop : prop.split("."); objArray.sort(function(a,b){ for (var p in propPath){ if (a[propPath[p]] && b[propPath[p]]){ a = a[propPath[p]]; b = b[propPath[p]]; } } // convert numeric strings to integers a = a.match(/^\d+$/) ? +a : a; b = b.match(/^\d+$/) ? +b : b; return ( (a < b) ? -1*direct : ((a > b) ? 1*direct : 0) ); }); } } sortJsonArrayByProperty(results, 'attributes.OBJECTID'); sortJsonArrayByProperty(results, 'attributes.OBJECTID', -1); 

更新:不要尝试

 function sortByProperty(objArray, prop, direction){ if (arguments.length<2) throw new Error("ARRAY, AND OBJECT PROPERTY MINIMUM ARGUMENTS, OPTIONAL DIRECTION"); if (!Array.isArray(objArray)) throw new Error("FIRST ARGUMENT NOT AN ARRAY"); const clone = objArray.slice(0); const direct = arguments.length>2 ? arguments[2] : 1; //Default to ascending const propPath = (prop.constructor===Array) ? prop : prop.split("."); clone.sort(function(a,b){ for (let p in propPath){ if (a[propPath[p]] && b[propPath[p]]){ a = a[propPath[p]]; b = b[propPath[p]]; } } // convert numeric strings to integers a = a.match(/^\d+$/) ? +a : a; b = b.match(/^\d+$/) ? +b : b; return ( (a < b) ? -1*direct : ((a > b) ? 1*direct : 0) ); }); return clone; } const resultsByObjectId = sortByProperty(results, 'attributes.OBJECTID'); const resultsByObjectIdDescending = sortByProperty(results, 'attributes.OBJECTID', -1); 

首先提取JSON编码的数据:

 var data = eval(yourJSONString); var results = data['results']; 

然后用自定义(用户)函数进行sorting:

 results.sort(function(a,b){ //return a.attributes.OBJECTID - b.attributes.OBJECTID; if(a.attributes.OBJECTID == b.attributes.OBJECTID) return 0; if(a.attributes.OBJECTID < b.attributes.OBJECTID) return -1; if(a.attributes.OBJECTID > b.attributes.OBJECTID) return 1; }); 

我以为你想按OBJECTIDsorting,但你可以改变它来sorting。

你可以通过提供一个自定义的比较函数作为Array.Sort()一个参数来sorting一个有序的数组。

 var myObject = /* json object from string */ ; myObject.results.sort(function (a, b) { // a and b will be two instances of your object from your list // possible return values var a1st = -1; // negative value means left item should appear first var b1st = 1; // positive value means right item should appear first var equal = 0; // zero means objects are equal // compare your object's property values and determine their order if (b.attributes.COMMERCIALNAME_E < a.attributes.COMMERCIALNAME_E) { return b1st; } else if (a.attributes.COMMERCIALNAME_E < b.attributes.COMMERCIALNAME_E) { return a1st; } else { return equal; } }); 

您无法对JSONstring进行sorting。JSON是数据传输的对象符号 – 即string。 你将不得不评估它作为一个对象字面量(例如与eval),并进行任何更改之前,再次重新序列化它。

从string中提取JSON

 var data = eval(given_JSON_string); var results = data['results']; 

通过传递自定义函数来sorting方法进行sorting

 results.sort(customfunction); 

自定义函数可以定义为

 function customfunction(a, b) { return a.attributes.COMMERCIALNAME_E < b.attributes.COMMERCIALNAME_E ? 1 : -1; 

}