Task

Write and test a function over an indented plain text outline which either:

 Returns a copy of the outline in which the sub-lists at every level of indentation are sorted, or
 reports that the indentation characters or widths are not consistent enough to make the outline structure clear.


Your code should detect and warn of at least two types of inconsistent indentation:

 inconsistent use of whitespace characters (e.g. mixed use of tabs and spaces)
 inconsistent indent widths. For example, an indentation with an odd number of spaces in an outline in which the unit indent appears to be 2 spaces, or 4 spaces.


Your code should be able to detect and handle both tab-indented, and space-indented (e.g. 4 space, 2 space etc) outlines, without being given any advance warning of the indent characters used, or the size of the indent units.

You should also be able to specify different types of sort, for example, as a minimum, both ascending and descending lexical sorts.

Your sort should not alter the type or size of the indentation units used in the input outline.



(For an application of Indent Respectful Sort, see the Sublime Text package of that name. The Python source text [1] is available for inspection on Github).


Tests

 Sort every level of the (4 space indented) outline below lexically, once ascending and once descending.
zeta
    beta
    gamma
        lambda
        kappa
        mu
    delta
alpha
    theta
    iota
    epsilon
 Do the same with a tab-indented equivalent of the same outline.
zeta
	gamma
		mu
		lambda
		kappa
	delta
	beta
alpha
	theta
	iota
	epsilon


The output sequence of an ascending lexical sort of each level should be:

alpha
    epsilon
    iota
    theta
zeta
    beta
    delta
    gamma
        kappa
        lambda
        mu
The output sequence of a descending lexical sort of each level should be:

zeta
    gamma
        mu
        lambda
        kappa
    delta
    beta
alpha
    theta
    iota
    epsilon
 Attempt to separately sort each of the following two outlines, reporting any inconsistencies detected in their indentations by your validation code.
alpha
    epsilon
	iota
    theta
zeta
    beta
    delta
    gamma
    	kappa
        lambda
        mu
zeta
    beta
   gamma
        lambda
         kappa
        mu
    delta
alpha
    theta
    iota
    epsilon


Related tasks

   Functional_coverage_tree
   Display_an_outline_as_a_nested_table


#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Sort_an_outline_at_every_level
use warnings;
 
for my $test ( split /^(?=#)/m, join '', <DATA> )
  {
  my ( $id, $outline ) = $test =~ /(\V*?\n)(.*)/s;
  my $sorted = validateandsort( $outline, $id =~ /descend/ );
  print $test, '=' x 20, " answer:\n$sorted\n";
  }
 
sub validateandsort
  {
  my ($outline, $descend) = @_;
  $outline =~ /^\h*(?: \t|\t )/m and
    return "ERROR: mixed tab and space indentaion\n";
  my $adjust = 0;
  $adjust++ while $outline =~ s/^(\h*)\H.*\n\1\K\h(?=\H)//m
    or $outline =~ s/^(\h*)(\h)\H.*\n\1\K(?=\H)/$2/m;
  $adjust and print "WARNING: adjusting indentation on some lines\n";
  return levelsort($outline, $descend);
  }
 
sub levelsort       # outline_section, descend_flag
  {
  my ($section, $descend) = @_;
  my @parts;
  while( $section =~ / ((\h*) .*\n) ( (?:\2\h.*\n)* )/gx )
    {
    my ($head, $rest) = ($1, $3);
    push @parts, $head . ( $rest and levelsort($rest, $descend) );
    }
  join '', $descend ? reverse sort @parts : sort @parts;
  }
 
__DATA__
# 4 space ascending
zeta
    beta
    gamma
        lambda
        kappa
        mu
    delta
alpha
    theta
    iota
    epsilon
# 4 space descending
zeta
    beta
    gamma
        lambda
        kappa
        mu
    delta
alpha
    theta
    iota
    epsilon
 
# mixed tab and space
alpha
    epsilon
  iota
    theta
zeta
    beta
    delta
    gamma
      kappa
        lambda
        mu
# off alignment
zeta
    beta
   gamma
        lambda
         kappa
        mu
    delta
alpha
    theta
    iota
    epsilon