A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Array.sort() - how to do it right?

  1. #1
    Member
    Join Date
    Jul 2001
    Posts
    32
    thank you for checking,

    i'm trying to sort an array with its .sort()-method.

    what i have is:
    39,58,18,-40,-19,-59

    after sorting it looks like this:
    -19,-40,-59,18,39,58

    now, that ain't right!

    but i want it right! how to?

    *marc

  2. #2
    Member
    Join Date
    Jul 2001
    Posts
    32
    ok, addition:

    i really can't work my head around it, duh...

    what i actually want to is, i have 6 MCs, generated with duplicateMovieClip, on level10 - 15.
    they move in a circle, but not flat on the screen. they#re moving in and out of the screen and in doing so, are changing position relative to each other. an MC that was covered by another one could by on top the next moment. clear?
    BUT I CAN'T WORK WITH swapDepth AND SORTING AND ALL, CAUSE I'M TOO STUPID!

    gimme a hint, pleeeeeeaaze!

  3. #3
    Senior Member
    Join Date
    May 2001
    Posts
    170
    try to use simple sorting function, it will work:


    function sortNumbers (a,b) {
    if (a < b) { return -1}
    if (a == b) {return 0}
    if (a > b) {return 1}
    }


    yourArray.sort(sortNumbers)


    Alex

  4. #4
    Member
    Join Date
    Jul 2001
    Posts
    32
    rock AND roll! alex, thanks.

    very smooth.

    haven't figured out that swapDepth-thing yet but i'm very optimistic.

    later, *marc


  5. #5
    Senior Member
    Join Date
    Feb 2001
    Posts
    112
    hi, your post caught my eye 'cos I was talking to somebody else about this

    see:

    http://board.flashkit.com/board/show...hreadid=242533


    You can't retrieve a mc's depth value from itself (unfortunately) so better to define right at the start and stick the values into an array. When I was doing something similiar I had a distance property, which was basically the distance from the viewer. This was the property that the array was sorted on, and the dpeths were then sorted out after that.

    Hope this helps

    Keith

  6. #6
    Member
    Join Date
    Jul 2001
    Posts
    32
    thanks, keith.

    yeah, guess you're right. i was hoping for something more...well less..., something.

    so, let's get going

    thanks a lot!

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