first check if it is an array, before trying to sort as we are calling 'length' on it