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

  • Home
  • SEARCH
  • 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 7248929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:12:34+00:00 2026-05-28T22:12:34+00:00

I have an array of objects: var countries = [ {‘id’: 35, ‘name’: ‘Перу’},

  • 0

I have an array of objects:

var countries = [
    {'id': 35, 'name': 'Перу'},
    {'id': 45, 'name': 'Індія'},
    {'id': 55, 'name': 'Єгипет'},
    {'id': 65, 'name': 'Албанія'},
]

I need to sort it by name. Here is my algorithm:

function mySort(s1, s2) {
    return s1.name.toString().localeCompare(s2.name.toString());
}

var sorted = countries.sort(mySort);

// result (incorrect)
sorted = [
    {'id': 55, 'name': 'Єгипет'},
    {'id': 45, 'name': 'Індія'},
    {'id': 65, 'name': 'Албанія'},
    {'id': 35, 'name': 'Перу'},
]

// must be
sorted = [
    {'id': 65, 'name': 'Албанія'},
    {'id': 55, 'name': 'Єгипет'},
    {'id': 45, 'name': 'Індія'},
    {'id': 35, 'name': 'Перу'},
]

What am I doing wrong?

Here is example on jsfiddle and screenshot of my result in jsfiddle.

enter image description here

Thanks!

EDIT:

I’ve found that only Google chrome sorts incorrect: FireFox and Opera do it correct.

  • 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-28T22:12:35+00:00Added an answer on May 28, 2026 at 10:12 pm

    Your code is working as you expected for me.

    It doesn’t make a functional difference, but you don’t need to call .toString() on something that is already a String. I.E., you can simplify mySort to this:

    var mySort = function(s1, s2){
        return s1.name.localeCompare(s2.name);
    }
    

    (I also prefer to declare all functions as variables, as they’re all the same in JavaScript – so they should be handled consistently.)

    However, as to answer why this may not be working for you, per http://msdn.microsoft.com/en-us/library/windows/apps/62b7ahzy%28v=vs.94%29.aspx (interestingly, MDN’s page doesn’t say anything about this) – emphasis mine:

    The localeCompare performs a locale-sensitive string comparison of the
    stringVar and the stringExp and returns -1, 0, or +1, depending on the
    sort order of the system default locale
    .

    If you want something that will sort purely on the character codes without regards to the user’s locale, you could use this instead:

    var mySort = function(s1, s2){
        if(s1 == s2){
          return 0;
        }
        return s1.name > s2.name;
    }
    

    This will sort consistently, regardless of the client’s or user’s settings / preferences – but it also returns the results you classified as incorrect.


    Edit: As you mentioned that the behavior you’re observing is specific to Google Chrome, this appears to be a known issue in Chrome, and is being tracked at http://code.google.com/p/v8/issues/detail?id=459.

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

Sidebar

Related Questions

I have an array of objects, like this: var companies = [ { name
I have an array of objects: var array = [(id, name, value),(id, name, value)];
I have an array of JSON objects like so: var myArray = [ {name:'foo',number:2},
I have an array of JavaScript objects: var objs = [ { first_nom: 'Laszlo',
I have code similar to this filtering entries in an Array of Objects: var
I have a map array with objects stuffed with variables looking like this: var
I have an Array of Objects that need the duplicates removed/filtered. I was going
I have an array of objects. Each object has a property called name. I
i have a varable: var name = name; this will be inside an array
Say I have an array of a few objects: var array = [{id: 1,

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.