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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:54:25+00:00 2026-05-31T13:54:25+00:00

I’m try to order objects numerically in an array in ascending order, by using

  • 0

I’m try to order objects numerically in an array in ascending order, by using a nested object property, the property being ‘score’.

The Array;

[ {name:'dan', score:220},
  {name:'lucy', score:876},
  {name:'mike', score:211} ]

I found the following thread, but have yet managed to get it working.

How to sort a JavaScript array of objects by nested object property?

Console outputs undefined.

function order_top_scores(prop, arr) {
    arr.sort(function (a, b) {
         if (a[prop] < b[prop]) {
        return -1;
         } else if (a[prop] > b[prop]) {
        return 1;
         } else {
        return 0;
         }
    });
};

function get_results(){
    $.get(wp_theme_dir+'/dottodot.php', 
         function(data){ 
        var returnedData = $.parseJSON(data);
        var ordered_scores = order_top_scores(returnedData)     
        console.log(returnedData);

    });
}

My Array differs slightly, could it be the second property thats disrupting the sort?
Or maybe the way i’m handling the data from the ajax request.

thanks in advance,
Cam

  • 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-31T13:54:26+00:00Added an answer on May 31, 2026 at 1:54 pm

    how are you?

    I’ve just tested this, and there’s a couple of things you might want to modify.

    First of all, you’re using the “sort” solution from the person who asked, not the actual solution, so you first need to rewrite your order_top_scores, like this:

            var order_top_scores = function (prop, arr,reverse) {
                if(!reverse) reverse = 0;
                prop = prop.split('.');
                var len = prop.length;
    
                arr.sort(function (a, b) {
                    var i = 0;
                    while( i < len ) { a = a[prop[i]]; b = b[prop[i]]; i++; }
                    if (a < b) {
                        return -1;
                    } else if (a > b) {
                        return 1;
                    } else {
                        return 0;
                    }
                });
    
                if(reverse){arr.reverse()};
                return arr;
            };
    

    Analyzing this function, I’ve added a third “reverse” parameter, which expects either true or false (because the original solution orders it from lowest to highest, and in this case you want the opposite)

    Now that you have this function, there’s 2 things you want to keep in mind:

    First

    In this line:

    var ordered_scores = order_top_scores(returnedData);
    

    You are not sending the first mandatory parameter, which actually tells the function which property you want the object sorted by: In this case, “score”.

    So, you have to call the function like this:

    var ordered_scores = order_top_scores('score',returnedData);
    

    And if you want it to be sorted from high to low, like this:

    var ordered_scores = order_top_scores('score',returnedData,true);
    

    Second

    Also, keep in mind you are outputting the “returnedData” value rather than the ordered_scores value so if this line:

    console.log(returnedData);
    

    Is outputting undefined, it means your JSON data is not correct. To be sure the sort worked, you should also output the ordered_scores like this:

    console.log(ordered_scores);
    

    Let me know if anything unclear.

    Cheers!

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i got an object with contents of html markup in it, for example: string
I am using Paperclip to handle profile photo uploads in my app. They upload

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.