|
-
Sorting an array of movie clips?
I seem to be having trouble getting my array sorted. It's an array of movie clips, which are linked to objects. The objects have a numeric field, called "rank", to which each has a unique value. If I try to do
Code:
myArray.sortOn(rank, Array.NUMERIC)
it doesn't seem to sort the array at all, but just randomizes it a little. I'm assuming that this is because it's an array of movie clips linked to objects, not an array of the objects themselves. Is there an easy way to solve this?
Edit: bah didn't see the new sticky, this is Flash 8
-
FK'n_dog
difficult to provide an answer without seeing the structure of your array 
does this approach help ??
Code:
arr =[];
arr[0]={name: "Jim", rank: 39}
arr[1]={name: "Joe", rank: 102}
arr[2]={name: "Jon", rank: 85}
arr.sortOn(rank);
arr.reverse();
/* --List Variables shows array--
_level0.arr = [object #1, class 'Array'] [
0:[object #2, class 'Object'] {
rank:102,
name:"Joe"
},
1:[object #3, class 'Object'] {
rank:85,
name:"Jon"
},
2:[object #4, class 'Object'] {
rank:39,
name:"Jim"
}
]
*/
-
Negatory.
I have an object in my library called "ThumbObj." In its linkage property, it's linked to an external actionscript class called Thumb.as.
The array is created in the main .fla, and movie clips are added to it like this:
Code:
_global.thumbnails.push( _root.attachMovie("ThumbObj","ThumbObj" + x, _root.getNextHighestDepth(), initObj) );
Inside of a for loop. It's pushed with a clone of the ThumbObj movieclip, named ThumbObj#, and it has an initObject to give it some properties.
The class the movie clip is attached to has an internable variable called "rank." I want to sort the array based on that variable, using the line
Code:
_global.thumbnails.sortOn(rank, Array.NUMERIC);
but it only seems to randomize the array. Putting rank in quotes doesn't work either.
Any idea what I'm doing wrong? Should it be sorting even though it's movieclips?
-
All 1s and 0s
Hi,
The sortOn method will only work on Arrays of objects. However, you can use sort() to sort arrays of any object, as long as you write the rule correctly. Here is the sort function you should use:
code:
function order(a, b):Number {
if (a.rank < b.rank) {
return -1;
} else if (a.rank > b.rank) {
return 1;
} else {
return 0;
}
}
Then to sort the array, just do:
code:
_global.thumbnails.sort(order);
Hope this helps.
"If I have seen further, it is by standing on the shoulders of giants." - Sir Isaac Newton
-
Ah, thank you very much. Now the only problem is that it's treating it like a string, aka it sorts 1, 10, 11, 12, 2, 3, 4, 41, etc. I can probably fix that on my own though. Thanks for the help, I knew you could do custom array sorts, but the concept always scared me.
-
All 1s and 0s
Hi,
You can add the Array.NUMERIC parameter and see if the helps, but it's more likely that you need to do a Number conversion in the order function:
code:
if (Number(a.rank) < Number(b.rank)) {
... //etc etc
"If I have seen further, it is by standing on the shoulders of giants." - Sir Isaac Newton
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
|