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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:50:18+00:00 2026-06-13T01:50:18+00:00

I have a table for categories like this: id parentid Title 1 0 Parent1

  • 0

I have a table for categories like this:

id parentid Title     
1  0        Parent1   
2  1        Child1        
3  0        Parent2
4  3        Child2
5  1        anotherChild1
6  3        anotherChild2

How can I sort it in this order:

Parent1
  child1
  anotherchild1
Parent2
  child2
  anotherchild2

and save the order into another field called sorted index?

  • 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-13T01:50:20+00:00Added an answer on June 13, 2026 at 1:50 am

    This should work for any level of hierarchy

    public void CustomSort()
    {
        List<MyNode> nodes = new List<MyNode>();
        nodes.Add(new MyNode() { Id = 5, ParentId = 1, Title = "Anotherchild1" });
        nodes.Add(new MyNode() { Id = 6, ParentId = 3, Title = "Anotherchild2" });
        nodes.Add(new MyNode() { Id = 7, ParentId = 6, Title = "Anotherchild3" });
        nodes.Add(new MyNode() { Id = 1, ParentId = 0, Title = "Parent1" });
        nodes.Add(new MyNode() { Id = 2, ParentId = 1, Title = "Child1" });
        nodes.Add(new MyNode() { Id = 3, ParentId = 0, Title = "Parent2" });
        nodes.Add(new MyNode() { Id = 4, ParentId = 3, Title = "Child2" });
    
        Func<MyNode, List<int>> hierarchy = null;
        hierarchy = n =>
        {
            var n2 = nodes.FirstOrDefault(x => x.Id == n.ParentId);
            if (n2 != null) return hierarchy(n2).Concat(new List<int>() { n.Id }).ToList();
            return new List<int>() { n.ParentId,n.Id };
        };
    
        var debug = nodes.Select(x=>hierarchy(x)).ToList();
    
        var sortedList = nodes.OrderBy(n => String.Join(",", hierarchy(n).Select(x=>x.ToString("X8"))))
                              .ToList();
    }
    
    class MyNode
    {
        public int Id;
        public int ParentId;
        public string Title;
    }
    

    Id  PId Title              hierarchy (key to sort)
    1   0   Parent1             0 1
    2   1   Child1              0 1 2
    5   1   Anotherchild1       0 1 5
    3   0   Parent2             0 3
    4   3   Child2              0 3 4
    6   3   Anotherchild2       0 3 6
    7   6   Anotherchild3       0 3 6 7
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a categories table that looks like this: ---------------------------------------- | id | parentId
Ok now i have two table categories and subcategories like this Categories: id title
I have an table structure like this mysql> SELECT id, name, parent_id FROM categories;
I have a Categories table in which each category has a ParentId that can
I have a table named categories like this: id int(11) NO PRI NULL auto_increment
I have Two tables like this: Table categories: columns: id, name, parent 1, Foods,
Ok so I have a table called categories that looks like this: id name
I have a categories table that looks like this: id | name | parent
I have a MySQL table like this: CREATE TABLE categories ( ID INT NOT
I have a categories table in MySql something like this: categoryId | categoryTitle |

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.