A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: [AS3]Array.sortOn not accurate?

  1. #1
    crossconscious
    Join Date
    Sep 2005
    Location
    Belgium
    Posts
    1,188

    [AS3]Array.sortOn not accurate?

    Ok, here's some code I used :

    Code:
    var transformedSurfaces:Array=surfaces.sortOn("depth",Array.DESCENDING|Array.NUMERIC);
    for(var i:int=0;i<transformedSurfaces.length;i++) {
    	trace(transformedSurfaces[i].depth);
    }
    And this is the result I got :


    Code:
    205.44603367212753
    182.78833899621128
    160.130644320295
    138.99973123326004
    138.99973123326004
    138.99973123326004
    138.99973123326004
    138.99973123326004
    138.99973123326004
    114.81525496846251
    114.81525496846251
    114.81525496846251
    92.15756029254628
    92.15756029254628
    90.63077870366499
    90.63077870366499
    90.63077870366499
    73.72604823403702
    69.49986561663002
    48.368952529595056
    48.36895252959505
    48.36895252959505
    48.36895252959505
    37.47373675257102
    37.47373675257102
    30.242796788862044
    30.242796788862044
    24.18447626479752
    24.18447626479752
    1.5267815888812777
    0
    0
    0
    0
    0
    -24.184476264797524
    -24.184476264797524
    -24.184476264797524
    -42.26182617406994
    -42.26182617406994
    -46.84217094071377
    -48.36895252959505
    -48.36895252959505
    -48.36895252959505
    0
    -82.178413468851
    -82.178413468851
    -82.178413468851
    -2.5357095704441965
    -8.452365234813989
    -89.10399711478371
    -90.63077870366499
    -90.63077870366499
    -21.130913087034973
    -31.696369630552457
    -132.89260487773493
    -132.89260487773493
    -137.47294964437876
    -138.99973123326006
    -138.99973123326006
    -138.99973123326006
    -157.07708114253245
    -172.80919217251602
    -172.80919217251602
    -172.80919217251602
    -181.26155740733
    -181.26155740733
    -181.26155740733
    -205.44603367212753
    -223.52338358139994
    -228.10372834804377
    -263.43997087618095
    -263.43997087618095
    -263.43997087618095
    -271.89233611099496
    -271.89233611099496
    -271.89233611099496
    -314.15416228506496
    -362.52311481465995
    -362.52311481465995
    -362.52311481465995
    -362.52311481465995
    -362.52311481465995
    -362.52311481465995
    -404.7849409887299
    -495.41571969239493
    -495.41571969239493
    -495.41571969239493
    -495.41571969239493
    -495.41571969239493
    -495.41571969239493
    -495.41571969239493
    Note that there's a zero out of place.

    I also tried a simple Array.sort(Array.DESCENDING|Array.NUMERIC), and had the toString() function of my objects return the depth field, and that way it was accurate but a lot slower.

    So, any ideas?

    Thanks.
    Last edited by Fall_X; 08-01-2007 at 02:25 PM.

  2. #2
    Feeling adventurous? T1ger's Avatar
    Join Date
    Mar 2004
    Posts
    850
    Post the input-array?
    I don't have a photograph, but you can have my footprints. They're upstairs in my socks.

  3. #3
    Senior Member
    Join Date
    Jan 2006
    Location
    USA
    Posts
    383
    Could this be your problem?

    EXFGHT% said on Jun 16, 2006 at 9:43 PM :

    Array.sortOn fails if the field to be sorted on in the underlying Object uses a property getter function, it will only work if the field is stored as a normal var.
    from here

  4. #4
    crossconscious
    Join Date
    Sep 2005
    Location
    Belgium
    Posts
    1,188
    Nope, the field is a normal var.

    The input var (only the values of the field I'm sorting on) :

    Code:
    -2.816829376715345
    -9.389431255717817
    -23.473578139294542
    -35.21036720894181
    34.19799306009999
    23.68865115012508
    -488.4209527080526
    -353.1790371435708
    -264.88427785767806
    -176.5895185717854
    -155.9157170681336
    -135.2419155644818
    -46.94715627858908
    88.29475928589271
    108.96856078954451
    129.64236229319633
    -488.4209527080526
    -353.1790371435708
    -255.49484660196026
    -197.2633200754372
    -176.5895185717854
    -88.2947592858927
    0
    20.673801503651806
    41.34760300730362
    129.64236229319633
    -488.4209527080526
    -353.1790371435708
    -255.49484660196026
    -129.6423622931963
    -167.20008731606757
    -41.34760300730362
    -78.90532803017489
    -20.67380150365181
    0
    64.82118114659816
    108.96856078954451
    129.64236229319633
    -488.4209527080526
    -353.1790371435708
    -255.49484660196026
    -129.6423622931963
    -167.20008731606757
    -41.34760300730362
    -78.90532803017489
    -20.67380150365181
    0
    20.673801503651806
    41.34760300730362
    129.64236229319633
    -488.4209527080526
    -353.1790371435708
    -264.88427785767806
    -129.6423622931963
    -167.20008731606757
    -41.34760300730362
    -78.90532803017489
    -20.67380150365181
    0
    88.29475928589271
    108.96856078954451
    129.64236229319633
    -488.4209527080526
    -353.1790371435708
    -264.88427785767806
    -176.5895185717854
    -88.2947592858927
    0
    88.29475928589271
    153.11594043249087
    197.2633200754372
    -488.4209527080526
    -400.1261934221599
    -311.83143413626715
    -223.53667485037448
    -135.2419155644818
    -46.94715627858908
    41.34760300730362
    129.64236229319633
    34.19799306009999
    69.51589677445708
    23.68865115012508
    0
    It's quite important that I find a way to fix this - when Im' using sort (in combination with toString()) I'm getting 40 fps, if I use sortOn I'm getting 110 fps. It's for my isometric engine thingie. Until now I've settled with the sort-approach, but off course speed is important here.
    Last edited by Fall_X; 08-01-2007 at 11:42 PM.

  5. #5
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    something must be wrong with your depth var because mine works:
    Code:
    var i:int = 500;
    var sortArray:Array = [];
    
    while(--i >= 0){
    	
    	var depth:Number = Math.random() * 1000 - 500;
    	
    	var ob:Object = {}
    	
    	if(Math.random() < .05) depth = 0;
    	
    	ob.depth = depth;
    	
    	sortArray.push(ob);
    }
    
    var transformArray:Array = sortArray.sortOn("depth", Array.NUMERIC | Array.DESCENDING);
    
    for(i = 0; i<transformArray.length; i++){
    	
    	trace(transformArray[i].depth)
    }
    lather yourself up with soap - soap arcade

  6. #6
    crossconscious
    Join Date
    Sep 2005
    Location
    Belgium
    Posts
    1,188
    I'm guessing it's some floating point accuracy problem.

    I managed to work around it by adding a number to the depth of my "sprites" (ie the objects that don't have to be skewed in the iso engine). My player character was always at coordinate 0,0,0 (in camera space), and it was flickering, ie sometimes it was underneath the tile he was standing on, and sometimes he was above it. If the difference between it's depth and the tile he was on was very small, the sort sometimes put them in the wrong order, but by increasing it's depth slightly, it works.

    Strange though, because it worked with the normal sort method.

  7. #7
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    otherwise you could write your own bubblesort function- can´t be so difficult

  8. #8
    crossconscious
    Join Date
    Sep 2005
    Location
    Belgium
    Posts
    1,188
    Yeah I've done that some years ago (in C if I'm not mistaking), but it can't be as fast as a native function.

  9. #9
    Senior Member
    Join Date
    Jan 2006
    Location
    USA
    Posts
    383
    Quote Originally Posted by renderhjs
    otherwise you could write your own bubblesort function- can´t be so difficult
    mergesort would probably be your best bet if you're looking for efficiency.

    A simple google search should lead you to the right direction, if you need to implement your own.

    I was looking on google.. but I'm not really sure how to find how the methods are implemented in AS3.

    I know that in Java.. the sort method.. uses mergesort.. and in order to do something like 'sorton'.. you just have to implement a Comparable interface and change the method to how you want to compare the objects.

    And I know in PHP and PERL, you can also customize how arrays are compared through the sort method.

    Not sure if that's possible in AS3.

  10. #10
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    http://linux.wku.edu/~lamonml/algor/sort/sort.html

    has source code too

    hmmm, me thinks a package is in order here
    Last edited by mr_malee; 08-02-2007 at 12:28 AM.
    lather yourself up with soap - soap arcade

  11. #11
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    what is the numeric value for the character "0"?
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  12. #12
    Senior Member Ray Beez's Avatar
    Join Date
    Jun 2000
    Posts
    2,793
    Could it be because you forgot the spaces? IE:

    surfaces.sortOn("depth",Array.DESCENDING | Array.NUMERIC)

  13. #13
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    hehe. i would say a day late and a dollar short but it's a lot longer than that lol
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  14. #14
    Senior Member Ray Beez's Avatar
    Join Date
    Jun 2000
    Posts
    2,793
    That's what she said

  15. #15
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    har har lmao!
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

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