admin管理员组文章数量:1188413
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
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
Share Improve this question asked May 8, 2010 at 11:19 ptrnptrn 5,2026 gold badges30 silver badges30 bronze badges 1- 2 possible duplicate of Sorting objects in an array by a field value in JavaScript, Javascript: How to sort an array of records by values in one of the fields? – outis Commented May 8, 2010 at 11:33
3 Answers
Reset to default 16Array#sort
(see section 15.4.4.11 of the spec, or MDC) accepts an optional function parameter which will be used to compare two entries for sorting purposes. The function should return -1 if the first argument is "less than" the second, 0 if they're equal, or 1 if the first is "greater than" the second. So:
outerArray.sort(function(a, b) {
var valueA, valueB;
valueA = a[1]; // Where 1 is your index, from your example
valueB = b[1];
if (valueA < valueB) {
return -1;
}
else if (valueA > valueB) {
return 1;
}
return 0;
});
(You can obviously compress that code a bit; I've kept it verbose for clarity.)
Here is a solution not needing a separate variable to contain the index
var arr = [.....]
arr.sort((function(index){
return function(a, b){
return (a[index] === b[index] ? 0 : (a[index] < b[index] ? -1 : 1));
};
})(2)); // 2 is the index
This sorts on index 2
Here used to be a sort implementation that returned the result of a simple x<y
comparison. This solution is disencouraged and this post is left only to preserve the ensuing discussion.
本文标签: algorithmsort outer array based on values in inner arrayJavaScriptStack Overflow
版权声明:本文标题:algorithm - sort outer array based on values in inner array, javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738394955a2084455.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论