用Javascript排序JSON对象

我一直在寻找排序这样的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",

   }

]}

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


ibeautiful
浏览 325回答 3
3回答

蝴蝶刀刀

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

月关宝盒

首先提取JSON编码的数据:var data = eval(yourJSONString);var results = data['results'];然后使用自定义(用户)函数进行排序:results.sort(function(a,b){&nbsp; &nbsp; //return a.attributes.OBJECTID - b.attributes.OBJECTID;&nbsp; &nbsp; if(a.attributes.OBJECTID == b.attributes.OBJECTID)&nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; if(a.attributes.OBJECTID < b.attributes.OBJECTID)&nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; if(a.attributes.OBJECTID > b.attributes.OBJECTID)&nbsp; &nbsp; &nbsp; &nbsp; return 1;});我假设您想按排序OBJECTID,但是您可以将其更改为按任何排序。

喵喔喔

您可以通过提供自定义比较功能作为的参数,对任何东西的有序数组进行排序Array.Sort()。var myObject = /* json object from string */ ;myObject.results.sort(function (a, b) {&nbsp; &nbsp; // a and b will be two instances of your object from your list&nbsp; &nbsp; // possible return values&nbsp; &nbsp; var a1st = -1; // negative value means left item should appear first&nbsp; &nbsp; var b1st =&nbsp; 1; // positive value means right item should appear first&nbsp; &nbsp; var equal = 0; // zero means objects are equal&nbsp; &nbsp; // compare your object's property values and determine their order&nbsp; &nbsp; if (b.attributes.COMMERCIALNAME_E < a.attributes.COMMERCIALNAME_E) {&nbsp; &nbsp; &nbsp; &nbsp; return b1st;&nbsp; &nbsp; }&nbsp; &nbsp; else if (a.attributes.COMMERCIALNAME_E < b.attributes.COMMERCIALNAME_E) {&nbsp; &nbsp; &nbsp; &nbsp; return a1st;&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; return equal;&nbsp; &nbsp; }});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript