#!/usr/bin/perl

# sort hostnames ordering compute nodes in human way

@lines = <>;

@sorted = sort {

    # both have two '-' chars?
    if ($a =~ /(.*)-(.*)-(.*)/){

	$a2 = $2; $a3 = $3;

	if ($b =~ /(.*)-(.*)-(.*)/){
	    
	    $b2 = $2; $b3 = $3;

	    if ($a2 == $b2 ) {
		# order by numeric sort of third field (rank)
		return $a3 <=> $b3;
	    } else {
		# order by middle field (rack)
		return $a2 <=> $b2;
	    }
	    
	}
	else {
	    # regular string comparison
	    return $a cmp $b;
	}
	
    }
    else {
	# regular string comparison
	return $a cmp $b;
    }

} @lines;

for (@sorted) {
    print ;
}
