Hello I have the following question:
I have a bidimensional array which look like this:
[[2, C, 22, 22, 8]
[2, C, 22, 22, 7]
[2, C, 22, 22, 10]
[1, R, 45, 45, 4]
[1, R, 45, 45, 3]
[1, R, 45, 45, 2]
[1, R, 45, 45, 1]
[1, R, 150, 100, 6]
[1, R, 150, 100, 5]
[1, C, 22, 22, 9]]
And I want to order the data first for column 1, then for column 2, then for column 3, then for column 4, then for column 5; all in descending order. The following is the result that I want to obtain:
[[2, C, 22, 22, 10]
[2, C, 22, 22, 8]
[2, C, 22, 22, 7]
[1, R, 150, 100, 6]
[1, R, 150, 100, 5]
[1, R, 45, 45, 4]
[1, R, 45, 45, 3]
[1, R, 45, 45, 2]
[1, R, 45, 45, 1]
[1, C, 22, 22, 9]]
I use ExtendScript which is an extended version of Javascript for Adobe. How is this possible with Javascript? Any suggestions would be much appreciated. Thanks in advance
You can use Array.prototype.sort
then loop through the options. (make sure to put quotes around your strings though!)
var x = [[2, "C", 22, 22, 8],
[2, "C", 22, 22, 7],
[2, "C", 22, 22, 10],
[1, "R", 45, 45, 4],
[1, "R", 45, 45, 3],
[1, "R", 45, 45, 2],
[1, "R", 45, 45, 1],
[1, "R", 150, 100, 6],
[1, "R", 150, 100, 5] ,
[1, "C", 22, 22, 9]];
x.sort(function(a,b){
for(var i=0; i<a.length; i++) {
if(a[i] > b[i]) {
return -1;
}
if(a[i] < b[i]) {
return 1;
}
}
return 0;
})
Note that I'm assuming that all arrays are the same length and that a simple <
and >
is sufficient for comparison. If your needs differ it should be trivial to adapt to that.
Working example: http://jsfiddle.net/zSHw9/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.