gpt4 book ai didi

javascript - 按 3 个属性对 javascript 数组进行排序

转载 作者:行者123 更新时间:2023-12-03 08:01:32 25 4
gpt4 key购买 nike

我有一个包含位置列表的数组。

我添加了 3 个属性,详细说明每个位置与设定的起始位置的关系。

数组中的每个位置都有以下 3 个属性:

bearing (0 to 360 degrees)
humanFriendlyCompassHeading (North, North East, East etc)
distance (in km)

我能够按方位对数组进行排序,以便位置从 0 到 360 度列出,并且人类友好的罗盘标题按顺序排列,因此数组中的前几个条目是北,其次是东北、东等。

但是,我希望将每个人类友好的罗盘方向(即北、东北、东)的距离从最近到最远进行排序。

这是我迄今为止使用下面提供的代码和比较函数所得到的结果:

var myCmp = composeComparisons([sortArrayByBearing, sortArrayByHumanFriendlyCompassHeading, sortArrayByDistance]);
res.geonames.sort(myCmp);


function sortArrayByDistance(A, B)
{
return parseFloat(A.distance) - parseFloat(B.distance);
}

function sortArrayByBearing(A, B)
{
return parseFloat(A.bearing) - parseFloat(B.bearing);
}

//Used to sort results array by humanFriendlyCompassHeading
//usage: results.sort(sortArrayByHumanFriendlyCompassHeading);
//The sorting of humanFriendlyCompassHeading is realised with a map and look up for the value.
function sortArrayByHumanFriendlyCompassHeading(A, B) {
var DIRECTIONS = { North: 0, NorthEast: 1, East: 2, SouthEast: 3, South: 4, SouthWest: 5, West: 6, NorthWest: 7};
return DIRECTIONS[A.humanFriendlyCompassHeading] - DIRECTIONS[B.humanFriendlyCompassHeading];
}

以下是我希望如何对数据进行排序的示例输出:

  • (514m,北)诺丁汉特伦特大学艺术与设计学院

  • (695m,北)诺丁汉植物园

  • (424m,东北)阿奇亚姆中心

  • (497m,东北)莎士比亚街卫斯理改革教堂

  • (795m,东北)诺丁汉市区

  • (796m,东北)诺丁汉维多利亚巴士站

  • (438m,东)诺丁汉 session 中心

这是原始数组的一部分。稍后,我将使用数组中返回的 lat 和 lng 值添加从我的起始位置开始的方位、距离和人类友好值:

         "summary":"The Diocese of Nottingham, England, is a Roman Catholic diocese of the Latin Rite which covers an area of 13,074 km², taking in the counties of Nottinghamshire (excluding the district of Bassetlaw), Leicestershire, Derbyshire, Rutland and Lincolnshire (...)",
"elevation":65,
"lng":-1.1572,
"distance":"0.0685",
"countryCode":"GB",
"rank":84,
"lang":"en",
"title":"Roman Catholic Diocese of Nottingham",
"lat":52.9545,
"wikipediaUrl":"en.wikipedia.org/wiki/Roman_Catholic_Diocese_of_Nottingham"
},
{ 
"summary":"The Cathedral Church of St. Barnabas in the city of Nottingham, England, is a cathedral of the Roman Catholic church. It is the mother church of the Diocese of Nottingham and seat of the Bishop of Nottingham. (...)",
"elevation":67,
"feature":"landmark",
"lng":-1.15708,
"distance":"0.0703",
"countryCode":"GB",
"rank":82,
"lang":"en",
"title":"Nottingham Cathedral",
"lat":52.95466,
"wikipediaUrl":"en.wikipedia.org/wiki/Nottingham_Cathedral"
},
{ 
"summary":"The Albert Hall, Nottingham, is a City Centre Conference and Concert venue, situated in Nottingham, England. (...)",
"elevation":61,
"feature":"landmark",
"lng":-1.1563944444444442,
"distance":"0.1217",
"countryCode":"GB",
"rank":72,
"lang":"en",
"title":"Albert Hall, Nottingham",
"lat":52.95441944444445,
"wikipediaUrl":"en.wikipedia.org/wiki/Albert_Hall%2C_Nottingham"
},
{ 
"summary":"The Nottingham Playhouse is a theatre in Nottingham, Nottinghamshire, England. It was first established as a repertory theatre in the 1950s when it operated from a former cinema. Directors during this period included Val May and Frank Dunlop (...)",
"elevation":67,
"feature":"landmark",
"lng":-1.1577,
"distance":"0.1235",
"countryCode":"GB",
"rank":77,
"lang":"en",
"title":"Nottingham Playhouse",
"lat":52.9537,
"wikipediaUrl":" en.wikipedia.org/wiki/Nottingham_Playhouseenter code here

最佳答案

编写与 Array.prototype.sort 一起使用的比较函数,您可以使用如下函数:

function composeComparisons(cmpFunctions) {
return function (a, b) {
for (var i = 0, result; i < cmpFunctions.length; i++) {
result = cmpFunctions[i](a, b);
if (result) {
return result;
}
}
return 0;
}
};

它将返回一个函数,该函数将项目与每个比较函数进行比较,直到获得显着的结果。

你会这样使用它:

// From most significant to least significant comparison function
var myCmp = composeComparisons([cmpByBearing, cmpByDistance, cmpByFriendliness]);

array.sort(myCmp);

提醒一下,如果 a 大于 b,则比较函数 cmp(a, b) 返回正数,如果 a 大于 b,则返回负数如果 b 大于 a,则为数字;如果项目相同,则为 0

关于javascript - 按 3 个属性对 javascript 数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34560396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com