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 4603120
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:05:47+00:00 2026-05-22T00:05:47+00:00

Usually while sorting you do: if (x < y) return -1 else if (x

  • 0

Usually while sorting you do:

if (x < y) return -1
else if (x > y) return 1
else return 0

or

return ((x > y) ? 1 : ((x < y)) ? -1 : 0))

Two comparisons for what seems can be accomplished with only one. In assembly all you have to do is subtract both to a register, check if is negative, check if is zero. The problem is in javascript if we were to subtract:

var sub = (x - y);
return (sub == 0 ? 0 : ((sub < 0) ? -1 : 1))

This would end up with even more code to be executed.

So, some questions:

  • Is there a way of simplifying or speedying this in javascript?
  • Can compiled javascript interpretors like chrome’s optimize this kind of comparison?
  • What about other languages?
  • 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-22T00:05:48+00:00Added an answer on May 22, 2026 at 12:05 am

    In Javascript, sort does not have to return -1, 0 or 1. It can return any number. This means that you only need to subtract one number from the other to compare them.

    The MDC docs for Array.sort suggest this implementation:

    function compareNumbers(a, b)
    {
      return a - b;
    }
    
    var nums = [34, 56, 1, 42, 63];
    nums.sort(compareNumbers);
    // [1, 34, 42, 56, 63]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple function that shows loading spinner while fetching data (usually takes
Usually one comes across this problem in python3.0 while attempting a split() method on
While reading about wireless technology, I usually meet the term provisioning. Anyone can help
Some URIs return a JSONObject object while others return a JSONArray . I usually
Usually, we can set this value as a text, while what I need is
I'm working on something and while I usually have no problems with c# i've
While project managers may each have their own personality and management style, it seems
Have been working in Ruby for a while, but usually in the context of
Usually one is looking for a way to update the UI while something in
During my job, I usually have to copy rows while changing their primary key

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.