【问题标题】:How can I sort my array alphabetically? [duplicate]如何按字母顺序对数组进行排序? [复制]
【发布时间】:2016-04-27 14:56:35
【问题描述】:

我正在使用 node.js。我想按他们的名字对这个数组进行排序,它的格式如下:

[["Kingston upon Thames",36.9],["Croydon",36.8],["Bromley",40.1],["Hounslow",35.4],["Ealing",35.9],["Havering",40.3],["Hillingdon",36.2],["Harrow",37.9],["Brent",35.4],["Barnet",37.1],["Lambeth",34.2],["Southwark",34.1],["Lewisham",34.8],["Greenwich",34.9],["Bexley",38.9],["Enfield",36.1],["Waltham Forest",34.7],["Redbridge",35.7],["Sutton",38.6],["Richmond upon Thames",38.5],["Merton",36.4],["Wandsworth",34.8],["Hammersmith and Fulham",35.4],["Kensington and Chelsea",38.9],["Westminster",37.4],["Camden",36],["Tower Hamlets",31.2],["Islington",34.6],["Hackney",32.8],["Haringey",34.8],["Newham",31.7],["Barking and Dagenham",32.9],["City of London",41.9]]

我想将其排序为 ["Barking and Dagenham",32.9],["Barnet",37.1],["Bexley",38.9] 等

【问题讨论】:

标签: javascript arrays json node.js


【解决方案1】:

只需获取您喜欢对其应用排序的元素。

array.sort(function (a, b) {
    return a[0].localeCompare(b[0]);
});

工作示例:

var array = [["Kingston upon Thames",36.9],["Croydon",36.8],["Bromley",40.1],["Hounslow",35.4],["Ealing",35.9],["Havering",40.3],["Hillingdon",36.2],["Harrow",37.9],["Brent",35.4],["Barnet",37.1],["Lambeth",34.2],["Southwark",34.1],["Lewisham",34.8],["Greenwich",34.9],["Bexley",38.9],["Enfield",36.1],["Waltham Forest",34.7],["Redbridge",35.7],["Sutton",38.6],["Richmond upon Thames",38.5],["Merton",36.4],["Wandsworth",34.8],["Hammersmith and Fulham",35.4],["Kensington and Chelsea",38.9],["Westminster",37.4],["Camden",36],["Tower Hamlets",31.2],["Islington",34.6],["Hackney",32.8],["Haringey",34.8],["Newham",31.7],["Barking and Dagenham",32.9],["City of London",41.9]];

array.sort(function (a, b) {
    return a[0].localeCompare(b[0]);
});

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

【讨论】:

  • 这是说错误return a[0].localeCompare(b[0]); 不是函数。我忘了提到我正在使用 node.js
  • @Luffydude 在 node.js 中运行良好
【解决方案2】:

使用Array.prototype.sort()

a = [["Kingston upon Thames",36.9],["Croydon",36.8],["Bromley",40.1],["Hounslow",35.4],["Ealing",35.9],["Havering",40.3],["Hillingdon",36.2],["Harrow",37.9],["Brent",35.4],["Barnet",37.1],["Lambeth",34.2],["Southwark",34.1],["Lewisham",34.8],["Greenwich",34.9],["Bexley",38.9],["Enfield",36.1],["Waltham Forest",34.7],["Redbridge",35.7],["Sutton",38.6],["Richmond upon Thames",38.5],["Merton",36.4],["Wandsworth",34.8],["Hammersmith and Fulham",35.4],["Kensington and Chelsea",38.9],["Westminster",37.4],["Camden",36],["Tower Hamlets",31.2],["Islington",34.6],["Hackney",32.8],["Haringey",34.8],["Newham",31.7],["Barking and Dagenham",32.9],["City of London",41.9]];
a.sort(function(a, b){
  var aName = a[0].toLowerCase();  
  var bName = b[0].toLowerCase();
  return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
});
console.log(a);

【讨论】:

    【解决方案3】:

    这种方式可能效率不高,但会达到您的目的。

    var myArray =[["Kingston upon Thames",36.9],["Croydon",36.8],["Bromley",40.1],["Hounslow",35.4],["Ealing",35.9],["Havering",40.3],["Hillingdon",36.2],["Harrow",37.9],["Brent",35.4],["Barnet",37.1],["Lambeth",34.2],["Southwark",34.1],["Lewisham",34.8],["Greenwich",34.9],["Bexley",38.9],["Enfield",36.1],["Waltham Forest",34.7],["Redbridge",35.7],["Sutton",38.6],["Richmond upon Thames",38.5],["Merton",36.4],["Wandsworth",34.8],["Hammersmith and Fulham",35.4],["Kensington and Chelsea",38.9],["Westminster",37.4],["Camden",36],["Tower Hamlets",31.2],["Islington",34.6],["Hackney",32.8],["Haringey",34.8],["Newham",31.7],["Barking and Dagenham",32.9],["City of London",41.9]]
    
    // Convert array of arrays to single json object
    var _tempArray = [];
    for(var i =0;i<myArray.length;i++){
    _tempArray.push({
        "name":myArray[i][0],
        "val":myArray[i][1]
      })
    }
    
    //sort it 
    var sortedArray = _tempArray.sort(function(a,b){
       return a.name >b.name?1:-1
    })
    console.log(sortedArray);
    

    jsfiddle 用于工作模型

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-17
      • 2017-01-17
      • 2011-11-04
      • 1970-01-01
      • 2012-10-05
      • 2013-11-02
      相关资源
      最近更新 更多