Open In App

p5.js sort() function

Improve
Improve
Like Article
Like
Save
Share
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.




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.




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.




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



Last Updated : 22 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads