Skip to content

Instantly share code, notes, and snippets.

@PabloAballe
Last active December 22, 2023 11:15
Show Gist options
  • Select an option

  • Save PabloAballe/84933f43e49434f8760cc75171acc55c to your computer and use it in GitHub Desktop.

Select an option

Save PabloAballe/84933f43e49434f8760cc75171acc55c to your computer and use it in GitHub Desktop.
Array Sorting Function: Efficient method for sorting arrays in JavaScript.
/**
* Array Sorting Function
* Brief Description: Efficient method for sorting arrays in JavaScript.
*
* Author: Pablo Aballe
* Date: 2023-12-22
*/
/**
* Sorts an array of numbers in ascending order.
* @param {number[]} array - The array of numbers to sort.
* @returns {number[]} - The sorted array.
*/
function sortArray(array) {
return array.sort((a, b) => a - b);
}
// Usage Example
console.log(sortArray([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]));
// Additional Notes:
// This method uses JavaScript's sort() function, which sorts the array in place.
// For sorting objects or more complex criteria, the comparison function can be adjusted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment