2016-01-01 99 views
0

我有一個包含位置列表的數組。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 
+1

你可以添加數據的一些例子嗎? –

+0

新增@NinaScholz –

+0

感謝您輸出的數據,但我們需要排序前的原始數據。 –

回答

1

要編寫比較函數爲使用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); 

作爲提醒,比較函數cmp(a, b)返回正數,如果a大於b,一個負數,如果ba大,0如果項目是相同的。

0

你應該排序順序

  • 軸承(0到360度)[無數據這裏]
  • humanFriendlyCompassHeading
  • 距離(以米爲單位)

可以與用排序來實現以下功能。

該函數爲每個排序組取得差異,如果它相同(這是值0),那麼將根據差異結果對下一個組進行排序,依此類推。

humanFriendlyCompassHeading(此處用compassDirection重新編號)的排序是用地圖實現的,並查找該值。

var data = [ 
 
    { distance: 695, compassDirection: 'North', target: 'The Arboretum, Nottingham' }, 
 
    { distance: 497, compassDirection: 'NorthEast', target: 'Shakespeare Street Wesleyan Reform Chapel' }, 
 
    { distance: 438, compassDirection: 'East', target: 'Nottingham Conference Centre' }, 
 
    { distance: 514, compassDirection: 'North', target: 'Nottingham Trent University, School of Art and Design' }, 
 
    { distance: 795, compassDirection: 'NorthEast', target: 'Nottingham Urban Area' }, 
 
    { distance: 424, compassDirection: 'NorthEast', target: 'Archiam Centre' }, 
 
    { distance: 796, compassDirection: 'NorthEast', target: 'Victoria bus station, Nottingham' } 
 
]; 
 

 
data.sort(function (a, b) { 
 
    var D = { North: 0, NorthEast: 45, East: 90, /* ... */ }; 
 
    return D[a.compassDirection] - D[b.compassDirection] || a.distance - b.distance; 
 
}); 
 

 
document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>');

相關問題