I think you want something like this:
arrays.sort((a,b) => a[0].localeCompare(b[0]))
More about localeCompare here
Try this version:
var arrays = [
['Ethan', '[email protected]', '[email protected]', '[email protected]'],
['David', '[email protected]', '[email protected]', '[email protected]'],
['Lily', '[email protected]', '[email protected]', '[email protected]'],
['Gabe', '[email protected]', '[email protected]', '[email protected]'],
['Ethan', '[email protected]', '[email protected]', '[email protected]'],
];
arrays.sort((arrA, arrB) => arrA[0].localeCompare(arrB[0]));
console.log(arrays);
Sort 2D array alphabetically?
javascript - How to sort 2 dimensional array by column value? - Stack Overflow
jquery - How to sort multidimensional array in javascript - Stack Overflow
How does this .sort / 2D array demo work?
The request is in the title. I can't seem to find anywhere how to rearrange a 2D in alphabetic order.
eg.
var data : Array = [
['Joseph', 100.90],
['Aaron', 78.0],
['Felix', 70.56],
['Alex', 390.00],
['Brian', 20.0]
]
var empty_array : Array
I want the new data in the empty_array to be:
empty_array = [
['Aaron', 78.0],
['Alex', 390.00],
['Brian', 20.0]
['Felix', 70.56],
['Joseph', 100.90]
]
What I want to do is rearrange the data array alphabetically and put them in the empty_array. The sort method is unfortunately a void return type. I'd imagine the for loop would be used in this case.
It's this simple:
var a = [[12, 'AAA'], [58, 'BBB'], [28, 'CCC'],[18, 'DDD']];
a.sort(sortFunction);
function sortFunction(a, b) {
if (a[0] === b[0]) {
return 0;
}
else {
return (a[0] < b[0]) ? -1 : 1;
}
}
I invite you to read the documentation.
If you want to sort by the second column, you can do this:
a.sort(compareSecondColumn);
function compareSecondColumn(a, b) {
if (a[1] === b[1]) {
return 0;
}
else {
return (a[1] < b[1]) ? -1 : 1;
}
}
The best approach would be to use the following, as there may be repetitive values in the first column.
var arr = [[12, 'AAA'], [12, 'BBB'], [12, 'CCC'],[28, 'DDD'], [18, 'CCC'],[12, 'DDD'],[18, 'CCC'],[28, 'DDD'],[28, 'DDD'],[58, 'BBB'],[68, 'BBB'],[78, 'BBB']];
arr.sort(function(a,b) {
return a[0]-b[0]
});
http://jsfiddle.net/VPrAr/

You can use the arry.sort(). The default is alphanumeric and ascending.
So it would be:
var arry = [ [1, "Convention Hall", "Dangalore", 10, "XYZ Company"],
[2, "Auditorium", "Belhi", 10, "ABC Company"],
[3, "CenterHall", "Aumbai", 10, "ZZZ Company"],
];
var x =arry.sort(function(a,b){ return a[2] > b[2] ? 1 : -1; });
alert(x);
Array.prototype.sort function expects a function as a parameter, which accepts two parameters and returns any of -1, 0 or 1.
I am a very big fan of functional programming, so I came up with this. This offers flexibility.
- You can sort based on any element
- You can even reverse the order of sorting
- You can customize, how the objects have to be compared.
function basicComparator(first, second) {
if (first === second) {
return 0;
} else if (first < second) {
return -1;
} else {
return 1;
}
}
function compareNthElements(n, comparatorFunction, reverse) {
return function(first, second) {
if (reverse === true) {
return comparatorFunction(second[n], first[n]);
} else {
return comparatorFunction(first[n], second[n]);
}
}
}
Thats it. Now, call the sort function like this
arry.sort(compareNthElements(1, basicComparator, true)); // Sorts first field and in reverse
arry.sort(compareNthElements(2, basicComparator)); // Sorts second field
Shortest possible code with ES6!
users.sort((a, b) => a.firstname.localeCompare(b.firstname))
String.prototype.localeCompare() basic support is universal!
Suppose you have an array users. You may use users.sort and pass a function that takes two arguments and compare them (comparator)
It should return
- something negative if first argument is less than second (should be placed before the second in resulting array)
- something positive if first argument is greater (should be placed after second one)
- 0 if those two elements are equal.
In our case if two elements are a and b we want to compare a.firstname and b.firstname
Example:
users.sort(function(a, b){
if(a.firstname < b.firstname) { return -1; }
if(a.firstname > b.firstname) { return 1; }
return 0;
})
This code is going to work with any type.
Note that in "real life"™ you often want to ignore case, correctly sort diacritics, weird symbols like ß, etc. when you compare strings, so you may want to use localeCompare. See other answers for clarity.