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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:21:27+00:00 2026-06-18T15:21:27+00:00

I have an hierarchical object on which I sort children by walking over the

  • 0

I have an hierarchical object on which I sort children by walking over the parents and sort the children. This works. But now, I need to optionally break the hierarchy and create new virtual restraints.

In order to illustrate this, let’s take an example of a Man, who has x Wifes. With each wife, he has y Kids. I can sort kids per wife, or wifes per man.

Man01   Wife01a     Kid01aA
                    Kid01aB
        Wife01b     Kid01bC
                    Kid01bD
Man02   Wife02c     Kid02cE
                    Kid02cF
        Wife02d     Kid02dG
                    Kid02dH

Let’s just give them names:

Murphy  Winnie  Kurt
                Kara
        Wendy   Klaus
                Klea
Marley  Wonda   Kasper
                Kyra
        Wilma   Kevin
                Karla

And think about sorting them alphabetically within their parents:

Marley  Wilma   Karla
                Kevin
        Wonda   Kasper
                Kyra
Murphy  Wendy   Klaus
                Klea
        Winnie  Kara
                Kurt

But now, we want to be able to sort the kids that belong to a man, or wifes in general, or kids in general?

Marley  Wilma   Karla
        Wonda   Kasper
        Wilma   Kevin
        Wonda   Kyra
Murphy  Winnie  Kara
        Wendy   Klaus
        Wendy   Klea
        Winnie  Kurt

This is an immensly simplified fictional object. In reality, in stead of sorting alphabetically, I do a multi-column-sort over many many properties.

It’s fine to output the results to a table, but processing itself already takes a lot of time and memory. I don’t want to complicate that further.

If that was no issue, I would just flatten the object as a table in an array, chain every multi-column sort into a super multi-column sort, and regroup starting at the closest common ancestor that was left intact with loops.

But I am trying to solve this in a more efficient way, without converting the object to a full-blown table-array.

  • How do I tacle this?
    actually looping over each and every one of them twice?

    • Perhaps there is a ‘well-known’ solution for this kind of sorting that I just don’t know about yet?
    • Maybe there is wizardry available creating table-like records using references for all ‘virtual’ parents, grouping these references back into hierarchy afterwards without looping over them?

Here’s an example of the kind of Object I am referring to:
By Object, I mean, quite literally {}, although the object contains arrays [] of objects {} when it has multiple members.

{
    "men"   : [
        {
            "name"  : "Murphy",
            // a lot of properties
            "wifes" : [
                {
                    "name"  : "Winnie",
                    // a lot of properties
                    "kids"  : [
                        {
                            "name"  : "Kurt",
                            // a lot of properties
                        }, {}, {} // etc...
                    ]
                }, {}, {} // etc...
            ]
        }, {}, {} // etc...
    ]
}

Note that in this case my example is wrong, because Man, Wife and Kid are all humans. But in reality there are different object with dissimilar properties. I should have chosen Universe, Planet, Soil or something, assuming there are multiple universes. 😉

  • 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-18T15:21:29+00:00Added an answer on June 18, 2026 at 3:21 pm

    we want to be able to sort the kids that belong to a man

    Then I would arrange them like this:

    Marley  Karla   Wilma   
            Kasper  Wonda
            Kevin   Wilma
            Kyra    Wonda
    Murphy  Kara    Winnie
            Klaus   Wendy
            Klea    Wendy
            Kurt    Winnie
    

    Of course, since there is only one mother per child there is no much difference, but for your actual data this might be different.

    Yet, you can already see now that you just have to sort each of the kids arrays per man.

    So, in general, instead of flattening to a large table array, multi-column sorting that, and regrouping the results as you suggested, you should do the grouping first and sort the groups afterwards – a bit like a bucket sort.

    var men = data["men"];
    men.forEach(function (man) {
        var kids = {};
        var wifes = man["wifes"];
        for (var i=0; i<wifes.length; i++) {
            var wkids = wifes[i]["kids"];
            for (j=0; j<wkids.length; j++) {
                var id = wkids[j]["name"];
                if (id in kids) {
                    kids[id].mothers.push(wifes[i]);
                else {
                    kids[id] = wkids[i];
                    kids[id].mothers = [ wifes[i] ];
                }
            }
        }
        // if the id is the sort criteria:
        man["kids"] = Object.keys(kids).sort().map(function(id) {
            return kids[id];
        });
        // else build the array first and then sort it:
        // man["kids"] = Object.values(kids).sort(function(kida, kidb) {
        //    <some kid comparison>
        // });
    
        // you might integrate this loop in the above, but it's independent:
        man["kids"].forEach(function(kid) {
            kid["mothers"].sort( /* some mother comparison */ );
        })
    });
    // now every man has a sorted "kids" array with each kid having a sorted "mothers" array
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have hundreds of properties that need to be set in an object, which
I have a highly structured hierarchical directory containing multiple files that need to be
I have three tables related to each other. They represent a hierarchical object. A_Table->
I have a repeater which binds a set of data. Within this repeater is
I have an object that contains sub properties, which also have sub properties and
I have a hierarchical data structure (using Entity beans)like say a Book Class which
I have a structured file with hierarchical text which describes a GUI in Delphi
I have an ASP.net TreeView control which displays a hierarchical folder structure. Each node
I have hierarchical data stored in the datastore using a model which looks like
I have an object with this structure public class Parent { public string Name

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.