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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:22:32+00:00 2026-06-17T09:22:32+00:00

Possible Duplicate: How to sort an array of javascript objects? I’m trying to convert

  • 0

Possible Duplicate:
How to sort an array of javascript objects?

I’m trying to convert some of my old ActionScript 2.0 code to JavaScript. Everything works fine but I am unable to sort my array numerically by score.

In my original ActionScript code I used:

// Sort the capitals array so that the capitals with the lowest score will be at the top
capitalsList.sortOn("score", Array.NUMERIC);

This worked fine but does not work in JavaScript. I changed it to:

capitalsList.sort("score", Array.NUMERIC);

But Alabama is still outputted (Connecticut should be outputted since its score is -1). What am I missing?

My script on JS Bin: http://jsbin.com/ibitez/1/edit

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script>
window.onload = function() {

    function capitalsConstructor(capitalName, stateName, score) {
        this.capitalName = capitalName;
        this.stateName = stateName;
        this.score = score;
    }

    // Create state capitals array
    var capitalsList = new Array();

    // All 50 capitals are loaded into the array with a score of 0
    // Sample limited to 10 capitals
    capitalsList[0] = new capitalsConstructor("Montgomery", "Alabama", 0);
    capitalsList[1] = new capitalsConstructor("Juneau", "Alaska", 0);
    capitalsList[2] = new capitalsConstructor("Phoenix", "Arizona", 0);
    capitalsList[3] = new capitalsConstructor("Little Rock", "Arkansas", 0);
    capitalsList[4] = new capitalsConstructor("Sacramento", "California", 0);
    capitalsList[5] = new capitalsConstructor("Denver", "Colorado", 0);
    capitalsList[6] = new capitalsConstructor("Hartford", "Connecticut", -1);
    capitalsList[7] = new capitalsConstructor("Dover", "Delaware", 0);
    capitalsList[8] = new capitalsConstructor("Tallahassee", "Florida", 0);
    capitalsList[9] = new capitalsConstructor("Atlanta", "Georgia", 0);

    capitalsList.sort("score", Array.NUMERIC);

    solution = capitalsList[0].stateName;

    document.getElementById("divSolution").innerText = solution;
}
</script>
</head>

<body>
<div id="divSolution"></div>
</body>
</html>
  • 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-06-17T09:22:32+00:00Added an answer on June 17, 2026 at 9:22 am

    You have to provide a callback to the sort method:

    capitalsList = capitalsList.sort(function (a, b) {
        return a.score - b.score;
    });
    

    Here’s the fiddle: http://jsfiddle.net/g2CQG/

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

Sidebar

Related Questions

Possible Duplicate: How to sort an array of javascript objects? Every time I mess
Possible Duplicate: How to sort an array of javascript objects? I have a array
Possible Duplicate: How to sort an array of javascript objects? i have a array
Possible Duplicate: Javascript Array.sort implementation? I want to know how .sort() works in JavaScript.
Possible Duplicate: How to sort an array of javascript objects? I have output that
Possible Duplicate: How to sort an array of javascript objects? Well, more preciselly, I
Possible Duplicate: Sort multidimensional array by multiple keys I am trying to find some
Possible Duplicate: Sort array of objects sort an object array in php EDIT: This
Possible Duplicate: Sorting objects in an array by a field value in JavaScript How
Possible Duplicate: Javascript array sort and unique I was surprised to see that there

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.