按3个属性对javascript数组排序
我有一个包含位置列表的数组 我添加了3个属性,详细说明了每个位置相对于一组起始位置的位置 阵列中的每个位置都具有以下3个属性:按3个属性对javascript数组排序,javascript,arrays,Javascript,Arrays,我有一个包含位置列表的数组 我添加了3个属性,详细说明了每个位置相对于一组起始位置的位置 阵列中的每个位置都具有以下3个属性: bearing (0 to 360 degrees) humanFriendlyCompassHeading (North, North East, East etc) distance (in km) 我能够按方位对阵列进行排序,因此位置从0到360度列出,并且人性化的指南针方向是有序的,因此阵列中的前几个条目是北方,然后是东北、东方等 但是,我希望每个人类友好罗盘
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];
}
以下是我希望如何对数据进行排序的示例输出:
- (北514米) 诺丁汉特伦特大学艺术与设计学院
- (北695米) 诺丁汉植物园
- (东北方向424米) 阿奇姆中心
- (497米,东北) 莎士比亚街卫斯理改革教堂
- (795米,东北方向) 诺丁汉市区
- (796米,东北) 维多利亚公共汽车站,诺丁汉
- (东面438米) 诺丁汉会议中心
"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
要编写用于的比较函数,可以使用以下函数:
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;
}
};
作为提醒,比较函数
cmp(a,b)
如果a
大于b
,则返回正数;如果b
大于a
,则返回负数;如果项目相同,则返回0
。
- 方位(0到360度)[此处无数据]
- 人性化社团
- 距离(米)
0
),则根据差异的结果获取下一个组并进行排序,依此类推
人性化CompassHeading
(此处用compassDirection
重新编写)的排序通过地图实现,并查找值
var数据=[
{距离:695,指南针方向:'北',目标:'诺丁汉植物园'},
{距离:497,compassDirection:'东北',目标:'莎士比亚街卫斯理改革教堂',
{距离:438,compassDirection:'东',目标:'诺丁汉会议中心'},
{距离:514,compassDirection:'北',目标:'诺丁汉特伦特大学,艺术与设计学院'},
{距离:795,compassDirection:'东北',目标:'诺丁汉市区'},
{距离:424,罗盘方向:'东北',目标:'阿奇姆中心'},
{距离:796,compassDirection:'东北',目标:'诺丁汉维多利亚汽车站'}
];
数据排序(函数(a,b){
变量D={北:0,东北:45,东:90,/*…*/};
返回D[a.compassDirection]-D[b.compassDirection]| | a.distance-b.distance;
});
document.write(''+JSON.stringify(数据,0,4)+'')代码>你能添加一些数据的例子吗?谢谢你的输出数据,但是在排序之前我们需要原始数据。我已经更新了我的原始问题。我已经尝试了你的代码,但仍然无法正确排序。我从geonames web API获取位置结果,然后使用返回的每个位置的lat和lng值计算并添加方位、距离和人性化指南针方向。方位在0到360之间人性化公司标题是北、东北、东、东南、南、西南、西或西北距离以公里为单位例如0.2999874请发布原始数组的一部分。在将原始JSON放到此处之前,请将其传递给类似的内容:
// From most significant to least significant comparison function
var myCmp = composeComparisons([cmpByBearing, cmpByDistance, cmpByFriendliness]);
array.sort(myCmp);