src/util/Util.js
/**
 * Sort the elements of the input array, in increasing alphabetical or numerical order.
 *
 * @name module:util.sort
 * @function
 * @public
 * @param {array} input - an array of numbers or of strings
 * @return {array} the sorted array
 * @throws if 'input' is not an array, or if its elements are not consistent in types, or if they are not all either numbers or
 * 	strings
 */
Sort the elements of the input array, in increasing alphabetical or numerical order.
Sort the elements of the input array in increasing alphabetical or numerical order .
function sort(input)
{
	const response = {
		origin: "util.sort",
		context: "when sorting the elements of an array",
	};
	try
	{
		if (!Array.isArray(input))
		{
			throw "the input argument should be an array";
		}
		const isNumberArray = input.every((element) => typeof element === "number");
		if (isNumberArray)
		{
			return input.sort((a, b) => (a - b));
		}
		const isStringArray = input.every((element) => typeof element === "string");
		if (isStringArray)
		{
			return input.sort();
		}
		throw "the input array should either consist entirely of strings or of numbers";
	}
	catch (error)
	{
		throw { ...response, error };
	}
}
function sort ( input ) { const response = { origin : "util.sort" , context : "when sorting the elements of an array" , } ; try { if ( ! Array . isArray ( input ) ) { throw "the input argument should be an array" ; } const isNumberArray = input . every ( ( element ) => typeof element === "number" ) ; if ( isNumberArray ) { return input . sort ( ( a , b ) => ( a - b ) ) ; } const isStringArray = input . every ( ( element ) => typeof element === "string" ) ; if ( isStringArray ) { return input . sort ( ) ; } throw "the input array should either consist entirely of strings or of numbers" ; } catch ( error ) { throw { ... response , error } ; } }
Sort the elements of the input array, in increasing alphabetical or numerical order.
Sort the elements of the input array in increasing alphabetical or numerical order .
// check the type and consistency of the array, and sort it accordingly: