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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:25:51+00:00 2026-05-25T03:25:51+00:00

The object var obj = { uniquestring1: { obj1: ‘content’, obj2: ‘content’ }, uniquestring2:

  • 0

The object

var obj = {
   uniquestring1:
   {
       obj1: 'content',
       obj2: 'content'
   },
   uniquestring2:
   {
       obj1: 'content'
   }
}

So i need to sort the object by number of the elements contained by the first parent element. The use of .sort function is not a sollution for me because i need to maintain the index assosiaction which array’s dont keep.

The result should be:

var obj = {
   uniquestring2:
   {
       obj1: 'content'
   },
   uniquestring1:
   {
       obj1: 'content',
       obj2: 'content'
   }
}
  • 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-25T03:25:52+00:00Added an answer on May 25, 2026 at 3:25 am

    Javscript objects don’t have a sort order so they can’t be sorted. To sort, you must have an array somewhere and I see no array in your code.

    Object keys have no defined order.

    • If you only need ordered access, use an array.
    • If you only need keyed access, use an object.
    • If you need access by both, you can use a parallel array, but this is a pain unless the data is not changing.

    You could do a one-time creation of a parallel array that contained key references in sorted order like this:

    var obj = {
       uniquestring1:
       {
           obj1: 'content',
           obj2: 'content'
       },
       uniquestring2:
       {
           obj1: 'content'
       }
    }
    
    var objOrder = [];   // sorted arrays that contains first level keys into obj
    var num;
    for (var i in obj) {
        objOrder.push(i);  // get all keys into the array
        num = 0;
        for (var j in obj[i]) {
            ++num;         // count items in each key object
        }
        obj[i].keyCnt = num;   // save this for later sorting purposes
    }
    // now sort the parallel array by keyCnt
    objOrder.sort(function(a, b) {
        return(obj[a].keyCnt - obj[b].keyCnt);
    });
    

    Then, to traverse the keys in sorted order, you would do it like this:

    for (var i = 0; i < sortedOrder.length; i++ ) {
        var item = obj[sortedOrder[i]];
        // do something with item here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an on object like so var obj = {}; function set() {
Is it like... var obj = new Object(); obj.function1 = function(){ //code } or
I Have an object with a set of parameters like: var obj = new
I have a JavaScript object like var obj = { key1: 'value1', key2: 'value2',
Given an object like this: var obj = { first:{ second:{ third:'hi there' }
I've got an object like this: var obj = { age:[23], part:[0], race:[], state:[AL],
If i have a JSON Object var Obj = { col1 : 'data' };
Here is my object literal: var obj = {key1: value1, key2: value2}; How can
I have a javascript object var obj = { 0 : apple, 1 :
I have a javascript object var obj = { 0 : apple, 1 :

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.