p5.js sort() function Last Updated : 22 Aug, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report The sort() function in p5.js is used to sort the array elements. If the array elements are strings then it sorts them alphabetically and if the array elements are integers then it sorts them in increasing order. Syntax: sort(Array, Count) Parameters: This function accepts two parameters as mentioned above and described below: Array: This parameter holds the array elements which to be sorted. Count: It holds the number of elements which need to be sorted. This should be less than the length of the array. Return Value: It returns a new sorted array. Below programs illustrates the sort() function in p5.js: Example 1: This example uses sort() function to sort the array elements. javascript function setup() { // Creating Canvas size createCanvas(500, 90); } function draw() { // Set the background color background(220); // Initializing the array let Array = ['IT', 'CSE', 'ECE', 'CIVIL']; // Initializing the Count which says // the number of elements to be sorted // from starting let Count = 3; // Calling to sort() function. let A = sort(Array, Count); // Set the size of text textSize(16); // Set the text color fill(color('red')); // Getting new sorted array text("New sorted array is : " + A, 50, 30); } Output: Example 2: This example uses sort() function to sort the array elements. javascript function setup() { // Creating Canvas size createCanvas(500, 90); } function draw() { // Set the background color background(220); // Initializing the array let Array = ['IT', 'CSE', 'ECE', 'CIVIL']; // Initializing the Count which says // the number of elements to be sorted // from starting let Count = 4; // Calling to sort() function. let A = sort(Array, Count); // Set the size of text textSize(16); // Set the text color fill(color('red')); // Getting new sorted array text("New sorted array is : " + A, 50, 30); } Output: Example 3: This example uses sort() function to sort the array elements. javascript function setup() { // Creating Canvas size createCanvas(500, 90); } function draw() { // Set the background color background(220); // Initializing the array let Array = [9, 6, 0, 22, 4, 1, 15]; // Initializing the Count which says // the number of elements to be sorted // from starting let Count = 5; // Calling to sort() function. let A = sort(Array, Count); // Set the size of text textSize(16); // Set the text color fill(color('red')); // Getting new sorted array text("New sorted array is : " + A, 50, 30); } Output: Reference: https://p5js.org/reference/#/p5/sort Comment More infoAdvertise with us Next Article Node.js sort() function K Kanchan_Ray Follow Improve Article Tags : JavaScript Web Technologies JavaScript-p5.js Similar Reads D3.js pie.sort() Function The pie.sort() function in D3.js is used to sort the data objects based on different properties of data. The comparator function can be used to define the basis on which the sorting would be done. Syntax: pie.sort( compare ) Parameters: This function accepts a single parameter as mentioned above and 4 min read Node.js sort() function sort() is an array function from Node.js that is used to sort a given array. Syntax: array_name.sort() Parameter: This function does not take any parameter. Return type: The function returns the sorted array. The program below demonstrates the working of the function: Program 1: javascript function 1 min read PHP sort() Function The sort() function is an inbuilt function in PHP and is used to sort an array in ascending order i.e, smaller to greater. It sorts the actual array and hence changes are reflected in the original array itself. The function provides us with 6 sorting types, according to which the array can be sorted 3 min read PHP usort() Function PHP comes with a number of built-in functions that are used to sort arrays in an easier way. Here, we are going to discuss a new function usort(). The usort() function in PHP sorts a given array by using a user-defined comparison function. This function is useful in case if we want to sort the array 2 min read PHP uasort() Function The uasort() function is a builtin function in PHP and is used to sort an array such that array indices maintain their correlation with the array elements they are associated with, using a user-defined comparison function. Syntax: boolean uasort(array_name, user_defined_function); Parameter: This fu 3 min read PHP uksort() Function The uksort() function is a built-in function in PHP and is used to sort an array according to the keys and not values using a user-defined comparison function. Syntax:boolean uksort($array, myFunction);Parameter: This function accepts two parameters which are described below: $array: This parameter 2 min read Like