Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6108229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:16:27+00:00 2026-05-23T14:16:27+00:00

I have a vector of fixed length 9 with some objects. each object has

  • 0

I have a vector of fixed length 9 with some objects. each object has a variable value:Point and the vector shall be sorted such that objects with a higher value.x appear first – and if theyr value.x is the same, then the one with the smaller value.y shall be first.

this is my compare function:

    private function cmpr(h1:HelpObj, h2:HelpObj):Number{ 
        var res:Number;
        if(h1.value.x==h2.value.x){
            res = h1.value.y-h2.value.y;
            return res;
        }
        else{
            res = h2.value.x-h1.value.x;
            return res;
        }
    }

but as you can see on this screenshot, the resulting order is not what it’s supposed to be:

problem

what am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:16:27+00:00Added an answer on May 23, 2026 at 2:16 pm

    Compare function recieves arguments a and b.

    It must return

    • -1 if a has to be placed before b
    • 0 if positions of b and a are equal
    • 1 if a has to be placed after b

    You are returning whatever values, not just those three listed, which is why order gets messed up.

    Compare the results in example below.

    var test:Vector.<Number>;
    
    // returning whatever the difference is
    var sortMethod1:Function = function (a:Number, b:Number) : Number
    {
        var value:Number = a-b;
        trace(a+"\t"+b +"\t= "+ value);
        return value;
    }
    
    // returning -1, 0, 1
    var sortMethod2:Function = function (a:Number, b:Number) : int
    {
        var result:int;
        if (a < b) {
            result = -1;
        } else if (a > b) {
            result =  1;    
        } else {
            result 0;
        }
        trace(a+"\t"+b +"\t: "+ result);
        return result;
    }
    
    test = new <Number>[1.2,1.1,1.4,1.5,0];
    trace("BEFORE", test);
    test.sort(sortMethod1);
    trace("AFTER1", test);
    
    trace("--");
    
    test = new <Number>[1.2,1.1,1.4,1.5,0];
    trace("BEFORE", test);
    test.sort(sortMethod2);
    trace("AFTER2", test);
    
    /*
    Trace output:
    
    BEFORE 1.2,1.1,1.4,1.5,0
    1.1 1.4 = -0.2999999999999998
    1.2 1.4 = -0.19999999999999996
    1.5 1.4 = 0.10000000000000009
    0   1.4 = -1.4
    0   1.4 = -1.4
    1.1 1.2 = -0.09999999999999987
    0   1.2 = -1.2
    1.5 1.2 = 0.30000000000000004
    1.5 1.2 = 0.30000000000000004
    0   1.2 = -1.2
    0   1.1 = -1.1
    AFTER1 0,1.1,1.2,1.5,1.4
    --
    BEFORE 1.2,1.1,1.4,1.5,0
    1.1 1.4 : -1
    1.2 1.4 : -1
    1.5 1.4 : 1
    0   1.4 : -1
    1.5 1.4 : 1
    0   1.4 : -1
    0   1.1 : -1
    1.1 1.2 : -1
    AFTER2 0,1.1,1.2,1.4,1.5
    */
    

    EDIT In your case that would be the following:

    private function cmpr(h1:HelpObj, h2:HelpObj):int
    { 
        var hx1:Number = h1.value.x;
        var hx2:Number = h2.value.x;
        if (hx1 < hx2) {       
            return -1;
        }
        if (hx1 > hx2) {       
            return 1;
        }
        var hy1:Number = h1.value.y;
        var hy2:Number = h2.value.y;
        if (hy1 < hy2) {       
            return -1;
        }
        if (hy1 > hy2) {       
            return 1;
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector-like class that contains an array of objects of type T
I have 2 vector of with one has vec1{e1,e2,e3,e4} and the other one with
I have fixed length char array used to store my decoded data. Once finish
I have a swing component which draws a fixed large (but fixed) vector image
I have vector of strings and want to create a fixed with string out
in clojure i have vector [myfn1 myfn2 myfn3] how can i call functions named
I have a vector that I want to insert into a set . This
I have a vector with 1000 nodes if(count + 1 > m_listItems.capacity()) m_listItems.reserve(count +
I have a vector of integers and I want to convert it to a
I have a vector of beans that holds information I want to display in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.