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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:18:50+00:00 2026-05-19T22:18:50+00:00

I have some JSON data that I get from a server. In my JavaScript,

  • 0

I have some JSON data that I get from a server. In my JavaScript, I want to do some sorting on it. I think the sort() function will do what I want.

However, it seems that JavaScript is converting the JSON data into an Object immediately on arrival. If I try to use the sort() method, I get errors a-plenty (using Firebug for testing).

I’ve looked around the net, and everyone seems to say that for one thing, JSON objects are already JavaScript arrays, and also that Objects can be treated just like arrays. Like over on this question, where in one of the answers, a guy says “The [Object object] is your data — you can access it as you would an array.”

However, that is not exactly true. JavaScript won’t let me use sort() on my object. And since the default assumption is that they’re all the same thing, there don’t seem to be any instructions anywhere on how to convert an Object to an Array, or force JavaScript to treat it as one, or anything like that.

So… how do I get JavaScript to let me treat this data as an array and sort() it?

Console log output of my object looks like this (I want to be able to sort by the values in the “level”):

OBJECT JSONdata

{ 
1: {
    displayName: "Dude1",
    email: "dude1@example.com<mailto:dude1@example.com>",
    lastActive: 1296980700, 
    level: 57, 
    timeout: 12969932837
}, 2: {
    displayName: "Dude2",
    email: "dude2@example.com<mailto:dude2@example.com>",
    lastActive: 1296983456,
    level: 28,
    timeout: 12969937382
}, 3: {
    displayName: "Dude3",
    email: "dude3@example.com<mailto:dude3@example.com>",
    lastActive: 1296980749,
    level: 99,
    timeout: 129699323459
} 
}
  • 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-19T22:18:51+00:00Added an answer on May 19, 2026 at 10:18 pm

    Array.prototype.slice.call(arrayLikeObject)

    is the standard way to convert and an array-like object to an array.

    That only really works for the arguments object. To convert a generic object to an array is a bit of a pain. Here’s the source from underscore.js:

    _.toArray = function(iterable) {
        if (!iterable)                return [];
        if (iterable.toArray)         return iterable.toArray();
        if (_.isArray(iterable))      return iterable;
        if (_.isArguments(iterable))  return slice.call(iterable);
        return _.values(iterable);
    };
    
    _.values = function(obj) {
        return _.map(obj, _.identity);
    };
    

    Turns out you’re going to need to loop over your object and map it to an array yourself.

    var newArray = []
    for (var key in object) {
        newArray.push(key);
    }
    

    You’re confusing the concepts of arrays and “associative arrays”. In JavaScript, objects kind of act like an associative array since you can access data in the format object["key"]. They’re not real associative arrays since objects are unordered lists.

    Objects and arrays are vastly different.

    An example of using underscore:

    var sortedObject = _.sortBy(object, function(val, key, object) {
        // return an number to index it by. then it is sorted from smallest to largest number
        return val;
    });
    

    See live example

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

Sidebar

Related Questions

I'm creating a program that gets data from a server using AJAX/JSON and then
I have a data Store that gets it's information from a JSON api on
I'm trying to generate a JSON response that includes some HTML. Thus, I have
We have some input data that sometimes appears with &nbsp characters on the end.
I have some kind of test data and want to create a unit test
I want to access some data from Google maps. the only way possible is
I use $.ajax() to get some HTML pages from my server. The retrun contains
I have a bit of data that I want to use to build a
I have some ajax functions on my default.aspx that I use for saving data
I am developing a web app that accesses some external JSON data. I'm currently

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.