-
Array sorting issue
I'm attempting to sort an array, which I've loaded from a sharedobject location. I've performed a simple sort on it, using a 'temp' string as a means of swapping items and it's saved the new array list in the process.
I want to be able to sort the array, but not overwrite the existing array with the new sorted order in the process, is there any way I can achieve this?
Thanks
Last edited by Gibson221; 12-11-2009 at 08:01 AM.
-
Flash/Flex Developer
Did you use the sort functions available with Array?
Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.
-
var x:Number = 1; x /= 0;
just create a new array and set it to the unsorted array. Then just sort that one.
-
Yeah, its part of my coursework and I'm to demonstrate an array sort algorithmically.
In terms of duplicating the array, ZippyDee, I tried something like that:
oldArray = dataStore.data.arraysort
var copyArray:Array = new Array();
copyArray = oldArray
then I used copyArray in the sort, but because its orginally equal to the oldArray, the oldArray gets overwritten as well
I'm probably missing something here and being really stupid, but i cant seem to figure this out...
thanks again
-
Ah, good old array copy . If you want to copy an array, the quickest way is to use slice(0) or concat()
This is what you're looking for:
Code:
myArray = [5,2,1,4,9,7];
myNewArray = myArray.slice(0);
myNewArray = myNewArray.sort();
trace (myArray);
trace (myNewArray);
You can do more reading here:
http://swingpants.com/2009/03/12/fas...cat-or-slice0/
Have fun!
-
Ah, that's fantastic Thank you, it worked
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
|