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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:58:47+00:00 2026-05-22T12:58:47+00:00

I have an object of objects, which I’d like to sort by property… having

  • 0

I have an object of objects, which I’d like to sort by property… having some trouble wrapping my head around it:

sample = {
    "Elem1": { title: "Developer", age: 33 },
    "Elem2": { title: "Accountant", age: 24 },
    "Elem3": { title: "Manager", age: 53 },
    "Elem4": { title: "Intern", age: 18}
}

My expected result would be an object whose keys were now ordered Elem4, Elem2, Elem1, Elem3. Alternatively, I’d be fine with simply returning the keys in that order rather than physically sorting the object.

Is this more trouble than it’s worth, or am I missing some obvious (or not-so-obvious) JavaScript-Fu that would make light work of something like this?

Thanks!

  • 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-22T12:58:47+00:00Added an answer on May 22, 2026 at 12:58 pm

    Properties (keys) of an object are not intrinsically ordered; you must maintain your own array of their ordering if you wish to do so.

    Here is an example of how you could simplify ordering your sample object by arbitrary properties via custom sort functions:

    var orderKeys = function(o, f) {
      var os=[], ks=[], i;
      for (i in o) {
        os.push([i, o[i]]);
      }
      os.sort(function(a,b){return f(a[1],b[1]);});
      for (i=0; i<os.length; i++) {
        ks.push(os[i][0]);
      }
      return ks;
    };
    
    orderKeys(sample, function(a, b) {
      return a.age - b.age;
    }); // => ["Elem4", "Elem2", "Elem1", "Elem3"]
    
    orderKeys(sample, function(a, b) {
      return a.title.localeCompare(b.title);
    }); // => ["Elem2", "Elem1", "Elem4", "Elem3"]
    

    Once the properties are ordered as you like then you can iterate them and retrieve the corresponding values in order.

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

Sidebar

Related Questions

I have some objects which will be used in the same manor. This object
Let's say I have objects which look very roughly like this: class object {
I have an Object X which contains a list of Objects X. I do
So I have an object which contains a set of objects in a private
If I have one object, 'Student', which will be associated with 2 'Course' objects,
I have this huge domain object(say parent) which contains other domain objects. It takes
I have several TextField columns on my UserProfile object which contain JSON objects. I've
I have parsed XML file into objects, in which each object has a 1:1
Image a couple of objects for which one property can have 3 possible values.
I have a list of objects which implement Comparable . I want to sort

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.