我有一個包含位置列表的數組。3個屬性對javascript數組排序
我正在添加3個屬性,詳細說明每個位置相對於設置的起始位置的位置。
陣列中的每個位置具有以下3個屬性:
bearing (0 to 360 degrees)
humanFriendlyCompassHeading (North, North East, East etc)
distance (in km)
我能夠通過軸承這樣的位置是從0至360度和人友好的羅盤標題列出於對數組進行排序是爲了因此陣列中的前幾個條目是North,接着是NorthEast,East等。
但是,我希望將距離從最近到最遠從每個人類友好指南針標題即北,東北,東。
這是我迄今使用下面提供的代碼和比較功能:
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米,北) 的ARBO retum,諾丁漢
(424米,東北) Archiam中心
(497米,東北) 莎士比亞街衛斯理改革教堂
(795米,東北) 諾丁漢都市區
(796m,NorthEast) 維多利亞汽車站,諾丁漢
(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
你可以添加數據的一些例子嗎? –
新增@NinaScholz –
感謝您輸出的數據,但我們需要排序前的原始數據。 –