'use strict';

// FUNCTIONS //

/**
* Returns a comparison result. If `-1`, `a` comes before `b`. If `1`, `b` comes before `a`. If `0`, the order stays the same.
*
* @private
* @param {string} a - first string
* @param {string} b - second string
* @returns {boolean} comparison result
*/
function comparator( a, b ) {
	if ( a === '__namespace__' ) {
		return -1;
	}
	if ( a[ 0 ] === '_' && b[ 0 ] === '_' ) {
		return comparator( a.substring( 1 ), b.substring( 1 ) );
	}
	if ( a[ 0 ] === '_' ) {
		return 1;
	}
	if ( b[ 0 ] === '_' ) {
		return -1;
	}
	if ( a < b ) {
		return -1;
	}
	if ( a > b ) {
		return 1;
	}
	return 0;
} // end FUNCTION comparator()


// MAIN //

/**
* Sorts tree keys.
*
* @private
* @param {StringArray} keys - keys
* @returns {StringArray} sorted keys
*/
function sort( keys ) {
	return keys.sort( comparator );
} // end FUNCTION sort()


// EXPORTS //

module.exports = sort;

@@ -355,24 +355,71 @@
 turn -1;%0A%09%7D%0A
+%09if ( b === '__namespace__' ) %7B%0A%09%09return 1;%0A%09%7D%0A
 %09if ( a%5B 0 %5D

Fix sort comparator