#!/bin/perl


$n = 0;
$entry = "";

while (<>) {

    if (/^#/) {
	$header .= $_;
	next;
    }

    if (/^\s+/) {
	$entry .= $_;
    }
    else {
	if ($entry ne "") {
	    if (($n % 1000) == 0) {
		#print "Processed $n entries\n";
	    }
	    $list[$n++] = $entry;
	}
	$entry = $_;
    }


}

#print "Sorting...\n";
$s= 0;
@sorted = sort sort_routine @list;

print $header;

foreach $e (@sorted) {
    print $e;
}

sub sort_routine {

    if (($s++ % 1000) == 0) {
	#print "$s Sort Computations\n";
    }

    $a =~ /.*\n.*\n\s+transitions=(.*)/;
    $trans = $1;
    $trans =~ /\s+\[up\s(\d+),\sdown\s(\d+),\schange\s(\d+)\]/;
    
    $total1 = $1 + $2 + $3;

    $b =~ /.*\n.*\n\s+transitions=(.*)/;
    $trans = $1;
    $trans =~ /\s+\[up\s(\d+),\sdown\s(\d+),\schange\s(\d+)\]/;

    $total2 = $1 + $2 + $3;

    return ($total2 <=> $total1);
}
