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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:16:33+00:00 2026-06-07T21:16:33+00:00

I need to write something that will take data that looks like this: B

  • 0

I need to write something that will take data that looks like this:

B
    b
    c
    a
A
    b
    a
D
    a
        b
        a   
C

And sort it like this:

A
    a
    b
B
    a
    b
    c
C
D
    a
        a
        b

The data looks exactly how I’m presenting it above (except for the letters). It is a multi-lined string where the number of tabs determine the level of hierarchy in the tree.

I want to be able to sort each level of hierarchy on it’s own.

I have been having trouble coming up with a decent algorithm so I’m asking here.

I am doing this in PHP, however any pseudo-code approach would be greatly appreciated.

Also, I realize that I can first build a tree and then sort and output that tree, but I am trying to find a more elegant solution.

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-06-07T21:16:36+00:00Added an answer on June 7, 2026 at 9:16 pm

    I actually solved this as I was in the process of asking, so I’ll answer my own question which may be helpful for somebody else on here. There are probably other good answers as well…

    class TreeLineSorter {
        function sort($tree_lines) {
            $sorted_line_groups = $this->group_and_sort_lines($tree_lines);
    
            return $this->get_sorted_lines($sorted_line_groups);
        }
    
        private function cmp_line_groups($a, $b) {
            return strcasecmp($a[0], $b[0]); 
        }
    
        private function get_line_level($line) {
            return strspn($line, "\t");
        }
    
        private function get_line_groups($lines) {
            $curr_level = $this->get_line_level($lines[0]);
            $line_groups = array();
            $idx = -1;
    
            foreach($lines as $line) {
                $level = $this->get_line_level($line);
    
                if ($level == $curr_level) {
                    $idx++;
                }
    
                $line_groups[$idx][] = $line;
            }
    
            return $line_groups;
        }
    
        private function group_and_sort_lines($lines) {
            $line_groups = $this->get_line_groups($lines);
    
            usort($line_groups, array($this,'cmp_line_groups'));
    
            foreach($line_groups as $key=>$group) {
                if (sizeof($group) > 1) {
                    $new_group = array(array_shift($group));
                    $new_group = array_merge($new_group, $this->group_and_sort_lines($group));
    
                    $line_groups[$key] = $new_group;
                }
            }
    
            return $line_groups;
        }
    
        private function get_sorted_lines($sorted_line_groups) {
            $lines = array();
    
            foreach($sorted_line_groups as $group) {
                if (is_array($group)) {
                    if (sizeof($group) > 1) {
                        $lines = array_merge($lines, $this->get_sorted_lines($group));
                    }
                    else {
                        $lines[] = $group[0];
                    }
                }
                else {
                    $lines[] = $group;
                }
            }
    
            return $lines;
        }
    }
    

    And here is the example usage:

        $sample_text = <<<QES
    B
    \tb
    \tc
    \ta
    A
    \tb
    \ta
    D
    \ta
    \t\tb
    \t\ta   
    C
    QES;
    
        $tree_lines = explode("\n",$sample_text);
    
        $tree_line_sorter = new TreeLineSorter();
    
        $sorted_tree_lines = $tree_line_sorter->sort($tree_lines);
    
        print_r($tree_lines);
        print_r($sorted_tree_lines);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that looks like this. I need to consistently write something
I need to write something like a minesweeper in prolog. I am able to
I'm trying to write some code that will take any object and convert it
I want to write a Java class that will take a CSV file and
We have some code that looks like this: class Serializer { public: template<class Type>
I need to write a bash script to do something as another user and
I need to write a generic class where the type parameter must be something
I need to write, in JavaScript (I am using jQuery), a function that is
I need to write a small console app (patch) that turns off the print
I need to write an app that reads a config file with info on

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.