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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:54:00+00:00 2026-06-15T14:54:00+00:00

I have an ajax call that returns a JSON object that is pretty complex

  • 0

I have an ajax call that returns a JSON object that is pretty complex and I’m having a hard time sorting it.

My call:

$.post('/reports-ajax',arguments, function(data) {}

The response:

{
"10001":{
    "unitname":"Fort Worth",
    "discounts":{"12-02-2012":"34.810000","12-03-2012":"20.810000","12-04-2012":"27.040000"},
    "gross":{"12-02-2012":"56.730000","12-03-2012":"19.350000","12-04-2012":"66.390000"},
    "net":{"12-02-2012":"61.920000","12-03-2012":"98.540000","12-04-2012":"39.350000"},
    "discounts_total":82.66,
    "gross_total":82.47,
    "net_total":99.81,
    "number":10001
    },
"10002":{
    "unitname":"Dallast",
    "discounts":{"12-02-2012":"12.600000","12-03-2012":"25.780000","12-04-2012":"47.780000","12-05-2012":"45.210000"},
    "gross":{"12-02-2012":"29.370000","12-03-2012":"91.110000","12-04-2012":"60.890000","12-05-2012":"51.870000"},
    "net":{"12-02-2012":"16.770000","12-03-2012":"65.330000","12-04-2012":"13.110000","12-05-2012":"06.660000"},
    "discounts_total":131.37,
    "gross_total":33.24,
    "net_total":101.87,
    "number":10002
    },
"32402":{
    "unitname":"Austin",
    "discounts":{"12-05-2012":"52.890000","12-02-2012":"22.430000","12-03-2012":"58.420000","12-04-2012":"53.130000"},
    "gross":{"12-05-2012":"25.020000","12-02-2012":"2836.010000","12-03-2012":"54.740000","12-04-2012":"45.330000"},
    "net":{"12-04-2012":"92.200000","12-05-2012":"72.130000","12-02-2012":"13.580000","12-03-2012":"96.320000"},
    "discounts_total":186.87,
    "gross_total":161.1,
    "net_total":174.23,
    "number":32402
    }
}

I go over the function with a standard each call and do some awesome stuff with highcharts but now I’m trying to sort the responses by the net_total call and I can’t figure it out.

I tried .sort() and it errors out that its not a function. I’ve been reading for a while but guess I’m not finding the right results. This looked promising: Sorting an array of JavaScript objects but it failed with the .sort is not a function. It seems most .sort are on [] arrays not full objects..

Any help would be greatly appreciated.

  • 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-15T14:54:01+00:00Added an answer on June 15, 2026 at 2:54 pm

    Sorting objects doesn’t make sense since object keys have no positional value. For example, this:

    { a:1, b:2 }
    

    and this:

    { b:2, a:1 }
    

    are exactly the same object. They’re not just similar, they’re the same.

    Nothing in javascript per se gives object keys any positional value. Some people perhaps are mistaken in the belief that:

    for (var key in obj) {
    

    iterates through the object keys in a specific sequence. But this is wrong. You should always assume that the for .. in loop processes object keys in random order, always, all the time.

    Obviously, if you’re going to write a web browser, you’re not going to implement a random number generator to parse a for .. in loop. Therefore most web browsers have an accidental stability to how the for .. in loop processes object keys.

    Developers who learn javascript by playing around with the browser may figure out that their browser iterates through objects in alphabetical order for example, or the order the keys were added to the object. But this is totally accidental and cannot be relied upon. The browser vendor may change this behavior in the future without violating any backwards compatability (except with buggy scripts written by people who believe objects have a sort order). Not to mention that different browsers have different implementations of javascript and therefore not necessarily have the same internal key ordering of objects.

    All the above is besides the point. “Key sort order” does not make any sense in javascript and any behavior observed is merely implementation detail. In short, javascript object does not have key order, just assume it’s random.


    Solution

    Now, what you’re really trying to do is not sort the object (you can’t, it doesn’t make sense). What you’re really trying to do is process the object attributes in a specific order. The solution is to simply create an array (which has sorting order) of object keys and then process the object using that array:

    // First create the array of keys/net_total so that we can sort it:
    var sort_array = [];
    for (var key in Response) {
        sort_array.push({key:key,net_total:Response[key].net_total});
    }
    
    // Now sort it:
    sort_array.sort(function(x,y){return x.net_total - y.net_total});
    
    // Now process that object with it:
    for (var i=0;i<sort_array.length;i++) {
        var item = Response[sort_array[i].key];
    
        // now do stuff with each item
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I have an ajax post that calls a service and returns an object
I have an ajax call that returns JSON data (Data Attached). After converting the
I'm using jQuery 1.8 and I have an ajax call that returns JSON. If
ok i have an ajax call that recives a json object. the idea of
I have a jQuery ajax call that returns html of a table. Now I
I have a web service that returns this string via the jQuery $.ajax() call
I want to call a web service that returns JSON object, the web service
In my JS I have the following ajax call, that bind the resulting json
I have an asp.net WebMethod that returns an XmlDocument object. I can successfully call
I have an ajax call to fetch information from Flickr API which returns JSON

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.