In Google Apps Script, the
sort() method is an array method that allows you to sort the elements of an array in ascending or descending order. It modifies the original array in place without creating a new array and returns the modified array.
sort() method uses a comparison function to determine the order of the elements in the sorted array. By default, the
sort() method sorts elements as strings in alphabetical and ascending order. However, you can specify a custom comparison function to sort elements in any order based on their values or properties.
Here is an example of using the
sort() method to sort an array of country names in alphabetical order:
var countries = ["USA", "Canada", "Mexico", "Brazil", "Argentina"]; // Sort the countries array alphabetically countries.sort(); // Output the sorted array Logger.log(countries); // Outputs: ["Argentina", "Brazil", "Canada", "Mexico", "USA"]
In this example, we call the
sort() method on the
countries array to sort its elements alphabetically. The original
countries array is modified in place, and the sorted array is outputted to the log using the