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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:23:20+00:00 2026-05-21T22:23:20+00:00

I did the following in javascript: var arr1 =[1,2,3,4]; var arr2 =[ac, bc, ad,

  • 0

I did the following in javascript:

var arr1 =[1,2,3,4];
var arr2 =["ac", "bc", "ad", "e"];
var result = arr1 .sort(function(i, j){return arr2[i].localeCompare(arr2[j])})
document.write(result );

my intention was to sort array1 based on array2. I was expecting the result to be 1,3,2,4, but as it turns out it is 2,1,3,4 can anyone figure out why? Thanks

  • 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-21T22:23:22+00:00Added an answer on May 21, 2026 at 10:23 pm

    Arrays are 0-indexed, so your sort function starts comparing with the second and all the way through the fifth; ignoring the first element and the fact that there is no 5th element.

    Inserting a -1 in the sort function should fix it:

    arr1.sort(function(i, j){
        return arr2[i-1].localeCompare(arr2[j-1])
    });
    

    The result is indeed [1, 3, 2, 4]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Did I not get enough sleep or what? This following code var frame=document.getElementById(viewer); frame.width=100;
The following javascript function used to work: function tbComment_onKeyDown(ID) { var commentPop = $find(WebDialogWindowComment)
Is there a way to write the following pure javascript in prototypejs var xhr
I have the following javascript code: <script type=text/javascript> $(function () { var currentDateTime =
In the following code snippet: var scr_h = $(document).height(); var scr_w = $(document).width(); var
I have the following JavaScript code: var name = ui.item.label.split('. ', 2); name.lastname =
I tried the following in JavaScript (Firefox 3.5, Windows XP): (function(){ window.foobar = 'Welcome!';
I have the following in my HTML5 code which makes use of javascript: var
I did the following to upper case the first letter in each word but
I did the following steps to use the CDialog in win 32 application: Changed

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.