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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:35:54+00:00 2026-05-11T20:35:54+00:00

I need to fill a tree-like UI control with some items (of course in

  • 0

I need to fill a tree-like UI control with some items (of course in parent-child1-child2-…childN relations) and before proceeding I want to ensure that the my collection that holds the content is ordered properly as follows:

Each object(in this case an instance of my Category class) from my collection (ObservableCollection that is not ordered) has a public property(a ParentCategoryID as string) that points to another ‘Category’ that will be its parent in my tree. A node in the tree can have 0 or any number of children.

So when filling the tree, each ‘category’ object that is to be displayed has already its ‘parent category'(based on the ParentCategoryID) in the collection.

What algorithm should I use to ensure that my collection is ordered this way before addig the elements in the tree?

  • 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-11T20:35:54+00:00Added an answer on May 11, 2026 at 8:35 pm

    I’m not sure if this is what you are looking for but hopefully it will help:

    using System;
    using System.Linq;
    using System.Collections;
    using System.Collections.Generic;
    public class Program
    {
       public static void Main(string[] args)
       {
          /* Tree Structure
    
             a
              d
               e   
             b
              f
              i
               j
             c
              g
               h
          */
    
          var a = new Category("a", null);
          var b = new Category("b", null);
          var c = new Category("c", null);
          var d = new Category("d", "a");
          var e = new Category("e", "d");
          var f = new Category("f", "b");
          var g = new Category("g", "c");
          var h = new Category("h", "g");
          var i = new Category("i", "b");
          var j = new Category("j", "i");
          var k = new Category("k", "z");
    
          var list = new CategoryCollection { k, j, i, h, g, f, e, d, c, b, a };
          foreach (var category in list.SortForTree())
          {
             Console.WriteLine("Name: {0}; Parent: {1}", category.Name, category.ParentCategoryID);
          }
       }
    }
    
    class Category
    {
       public string ParentCategoryID { get; set; }
       public string Name { get; set; }
       public Category(string name, string parentCategoryID)
       {
          Name = name;
          ParentCategoryID = parentCategoryID;
       }
    }
    
    class CategoryCollection : IEnumerable<Category>
    {
       private List<Category> list = new List<Category>();
    
       public void Add(Category category)
       {
          list.Add(category);
       }
    
       public IEnumerable<Category> SortForTree()
       {
          var target = new Dictionary<string, Category>();
    
          SortForTree(list, target);
    
          return target.Values;
       }
    
       private void SortForTree(List<Category> source, Dictionary<string, Category> target)
       {
          var temp = new List<Category>();
    
          foreach (var c in source)
          {
             if (c.ParentCategoryID == null || (target.ContainsKey(c.ParentCategoryID) && !target.ContainsKey(c.Name)))
             {
                target.Add(c.Name, c);
             }
             else
             {
                if (source.Exists(o => o.Name == c.ParentCategoryID))
                {
                   temp.Add(c);
                }
             }
          }
    
          if (temp.Count > 0) SortForTree(temp, target);
       }
    
       #region IEnumerable<Category> Members
    
       public IEnumerator<Category> GetEnumerator()
       {
          return list.GetEnumerator();
       }
    
       #endregion
    
       #region IEnumerable Members
    
       IEnumerator IEnumerable.GetEnumerator()
       {
          return list.GetEnumerator();
       }
    
       #endregion
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need to fill a listbox with items one by one like the list
I need to fill in a rect with some text. The size of rect
I need to fill a String to a certain length with dashes, like: cow-----8
I'm trying to make changes to some legacy code. I need to fill a
Looking for some examples of iText 5 and ColdFusion 7. I need to fill
I need to fill a window with pixels, very fast of course. This is
I need to fill some dropdown boxex from some reference data. i.e City List,
I need to add a facebook like button to menu items from a php
I need to fill two DataTable from two different databases for some comparisons and
I need to fill an panel with clickable button-based cells that looks like this:

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.