|
-
Hi All
I have a long list off arrays i need to sort using one off the arrays as sort of a key. I'm using it as a kind of address-database
_level0.arrTelefon
_level0.arrTelefax
_level0.arrName
Right now all data is sorted in different arrays with
"_level0.arrTelefon" as the 'key'.
How can I rearrange the arrays - using the alphabetical order in array "_level0.arrName" as a key?
Regards
Podenphant
-
ism
you can sort an array using a function to determine the sort sequence for example you could have an array of objects;
Code:
arr = [ { name: "bill", age: 21}, {name: "jim", age: 18 }, { name: "pam", age: 24 }, { name: "jill", age 19 } ];
then you can sort that array using the sort function;
Code:
arr.sort(mySequence);
where mySequence is a function defined by you that is passed two elements of the array. the function deterimines the order of the elements it is passed and returns the value;
-1 if A appears before B in the sorted sequence
0 if A = B
1 if A appears after B in the sorted sequence
Code:
function mySequence(a, b) {
if (a.age > b.age)
return(-1)
else
if (a.age < b.age)
return(1);
return(0);
}
this should sort the array in descending sequence based on the persons age.
hope this helps
BlinkOk
-
You can make an array of object to combine 3 array together. After sort that array, then you break them apart into 3 arrays.
Or you can make a temporal array of "index"; Then sort the index to get sequence and create 3 new array according to this new sequence of index.
Code:
_level0.arrTelefon = ["040", "080", "060"];
_level0.arrTelefax = ["T", "V", "U"];
_level0.arrName = ["A", "C", "B"];
temp = [];
for (var p = 0; p<arrName.length; p++) {
temp[p] = p;
}
function order(a, b) {
if (arrName[a]>arrName[b ]) {
return 1;
}
}
temp.sort(order);
arr1 = [];
arr2 = [];
arr3 = [];
for (var k = 0; k<temp.length; k++) {
var index = temp[k];
arr1[k] = arrName[index];
arr2[k] = arrTelefon[index];
arr3[k] = arrTelefax[index];
}
arrName = arr1;
arrTelefon = arr2;
arrTelefax = arr3;
trace(arrName);
trace(arrTelefon);
trace(arrTelefax);
-
thx for the code ericlin
however there are some syntax problems that I can't figure out ... could you help out?
-
I dont know how to fix the display.
Try:
click "Reply w/Quote"; Then the content of the post will be quoted in the white edit box. There you can see and copy the raw codes.
-
Thx ericlin, works like a dream
since we're at it ...
for a highscore I have a long list of arrays containing "name" "score" and "email"
How can I sort and list each array by score? And list X number of players?
Score 1
Score 2
... and so on
-
Code:
//show the first 2 highest score data
player1 = ["pl1", 520, "[email protected]"];
player2 = ["pl2", 120, "[email protected]"];
player3 = ["pl3", 100, "[email protected]"];
player4 = ["pl4", 900, "[email protected]"];
list = [];
for (var p in _root) {
var arr = _root[p];
if ((typeof (arr) == "object") && (p.indexOf("player")>=0)) {
list.push(arr);
}
}
function order(a, b) {
if (a[1]<b[1]) {
return 1;
}
}
list.sort(order);
X = 2;
for (var k = 0; k<X; k++) {
trace(list[k]);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|