• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Vector.<Point>.sortOn()? How to make a compare function?

LEGEND ,
May 29, 2009 May 29, 2009

Copy link to clipboard

Copied

Okay in AS2 I had an array of Points. And I sorted it like this: myArray.sortOn(["x", "y"],[Array.NUMERIC, Array.NUMERIC]);

I was changing the code over to AS3 and I thought I would change my array into a Vector of Points. But then I realized there isn't a sortOn method for Vectors.

The sort method takes a compare function and I think I have that working. But I was wondering if there is a better or cleaner way of writing compare functions? This is new to me.

function compare(p1:Point,p2:Point):Number {

if (p1.x>p2.x || (p1.x==p2.x && p1.y>p2.y)) {

return 1;

} else if (p1.x<p2.x || (p1.x==p2.x && p1.y<p2.y)) {

return -1;

}

return 0;

}

TOPICS
ActionScript

Views

5.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 30, 2009 May 30, 2009

Copy link to clipboard

Copied

I feel sure that using the faster Vector arrays and doing your own sort won't be as fast as using an Array and doing a sortOn. Also, it would limit it to Flash Player 10, which although fairly common by now, isn't everywhere like player 9 is.

Your original AS2 code works unchanged in AS3:

var myArray:Array = [new Point(10,10),new Point(10,5),new Point(5,33)]

myArray.sortOn(["x", "y"],[Array.NUMERIC, Array.NUMERIC]);

trace(myArray)

(x=5, y=33),(x=10, y=5),(x=10, y=10)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 30, 2009 May 30, 2009

Copy link to clipboard

Copied

there's no substantial difference but in my tests (in cs4,as3) the array.sortOn() was slightly faster:

var v:Vector.<Point>;
v = new Vector.<Point>();

var lengthV:uint=1000000;
for (var i:uint=0; i<lengthV; i++) {
    v.push(new Point(Math.round(i*Math.random()),Math.round(i*Math.random())));
}


var startTime:uint = getTimer();
v.sort(f);
trace(getTimer()-startTime);  // ~4700

var a:Array = [];
for(var j:uint=0;j<lengthV;j++){
    a.push(new Point(Math.round(i*Math.random()),Math.round(i*Math.random())));
}


startTime = getTimer();
a.sortOn(["x", "y"],[Array.NUMERIC, Array.NUMERIC]);
trace(getTimer()-startTime);  // ~3500

function f(p1:Point,p2:Point):Number {
    if (p1.x>p2.x || (p1.x==p2.x && p1.y>p2.y)) {
        return 1;
    } else if (p1.x<p2.x || (p1.x==p2.x && p1.y<p2.y)) {
        return -1;
    }
    return 0;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 30, 2009 May 30, 2009

Copy link to clipboard

Copied

So would you agree that adding several more lines of code, making it 25% slower, and giving up Flash 9 support, wouldn't be incentives for doing it that way!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 30, 2009 May 30, 2009

Copy link to clipboard

Copied

well, you sure wouldn't use the vector class because of benefits in sorting.  you would use it because of other benefits, if those benefits offset the drawback of requiring fp 10.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 30, 2009 May 30, 2009

Copy link to clipboard

Copied

Thanks for the ideas. Yeah I felt that the Vector usage wasn't a bonus, but was too lazy to test it last night!

But assuming that for any other reason I did have/want to use Vector, what do you think of the compare function? Do either of you see any better way to write that?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 30, 2009 May 30, 2009

Copy link to clipboard

Copied

no, i don't think you can do much better than that.  splitting your if-statements might make a little difference for better or for worse, but i can't imagine it would be significant.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 22, 2010 Mar 22, 2010

Copy link to clipboard

Copied

LATEST

This is about 5.6 times slower than array.sortOn

private var _prop:String;

public final function sortOn(prop:String) : void
{
     _prop = prop;
     vector.sort(sorter);
}

private final function sorter(obj1:Object,obj2:Object) : Number
{
     var a:Object = obj1[_prop];
     var b:Object = obj2[_prop];
     if (a < b)
          return -1;
     if (a > b)
          return 1;
     return 0;
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines