I have an array with a list of locations.
I am adding on 3 properties detailing where each location is in relation to a set start location.
Each location in the array has the following 3 properties:
bearing (0 to 360 degrees)
humanFriendlyCompassHeading (North, North East, East etc)
distance (in km)
I am able to sort the array by bearing so the locations are listed from 0 to 360 degrees and the human friendly compass headings are in order so the first few entries in the array are North, followed by NorthEast, East etc.
However, I would like the distances to be then sorted from nearest to furthest away for each human friendly compass heading i.e. North, NorthEast, East.
This is what I have so far using the code provided below and the comparison function:
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];
}
Here is a sample output of how I would like the data to be sorted:
(514m, North) Nottingham Trent University, School of Art and Design
(695m, North) The Arboretum, Nottingham
(424m, NorthEast) Archiam Centre
(497m, NorthEast) Shakespeare Street Wesleyan Reform Chapel
(795m, NorthEast) Nottingham Urban Area
(796m, NorthEast) Victoria bus station, Nottingham
(438m, East) Nottingham Conference Centre
This is part of the original array. I am adding on the bearing, distance and human friendly values from my start location later on using the lat and lng values returned in the array:
"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