sort outer array based on values in inner array, javascript
Posted
by ptrn
on Stack Overflow
See other posts from Stack Overflow
or by ptrn
Published on 2010-05-08T11:19:23Z
Indexed on
2010/05/08
11:28 UTC
Read the original article
Hit count: 320
I have an array with arrays in it, where I want to sort the outer arrays based on values in a specific column in the inner.
I bet that sounded more than a bit confusing, so I'll skip straight to an example.
Initial data:
var data = [
[
"row_1-col1",
"2-row_1-col2",
"c-row_1-coln"
],
[
"row_2-col1",
"1-row_2-col2",
"b-row_2-coln"
],
[
"row_m-col1",
"3-row_m-col2",
"a-row_m-coln"
]
];
Sort data, based on column with index 1
data.sortFuncOfSomeKind(1);
where the object then would look like this;
var data = [
[
"row_2-col1",
"1-row_2-col2",
"b-row_2-coln"
],
[
"row_1-col1",
"2-row_1-col2",
"c-row_1-coln"
],
[
"row_m-col1",
"3-row_m-col2",
"a-row_m-coln"
]
];
Sort data, based on column with index 2
data.sortFuncOfSomeKind(2);
where the object then would look like this;
var data = [
[
"row_m-col1",
"3-row_m-col2",
"a-row_m-coln"
],
[
"row_2-col1",
"1-row_2-col2",
"b-row_2-coln"
],
[
"row_1-col1",
"2-row_1-col2",
"c-row_1-coln"
]
];
The big Q
Is there an existing solution to this that you know of, or would I have to write one myself? If so, which would be the easiest sort algorithm to use? QuickSort?
_L
© Stack Overflow or respective owner