A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Array sorting issue

  1. #1
    Member
    Join Date
    Feb 2007
    Posts
    50

    Post 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.

  2. #2
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813
    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.

  3. #3
    var x:Number = 1; x /= 0;
    Join Date
    Dec 2004
    Posts
    549
    just create a new array and set it to the unsorted array. Then just sort that one.
    Z¡µµ¥ D££

    Soup In A Box

  4. #4
    Member
    Join Date
    Feb 2007
    Posts
    50
    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

  5. #5
    Member
    Join Date
    Aug 2003
    Posts
    70
    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!

  6. #6
    Member
    Join Date
    Feb 2007
    Posts
    50
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center