1

I'm using node.js. I want to sort this array by their names, it has the following format:

[["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]]

I want to sort it like ["Barking and Dagenham",32.9],["Barnet",37.1],["Bexley",38.9],etc

Luffydude
  • 702
  • 14
  • 27
  • [site:stackoverflow.com javascript sort arrays of arrays of strings](https://www.google.com/search?q=site%3Astackoverflow.com+javascript+sort+arrays+of+arrays+of+strings&gws_rd=ssl) –  Apr 27 '16 at 15:01

3 Answers3

4

Just take the elements, on which you like to apply sort.

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

Working example:

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>');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

Use 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);
rrk
  • 15,677
  • 4
  • 29
  • 45
1

This way may not be efficient, but it will serve your purpose.

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 for working model

brk
  • 48,835
  • 10
  • 56
  • 78