Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

p5.js | sort() function

  • Last Updated : 16 Apr, 2019
Geek Week

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

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!




My Personal Notes arrow_drop_up
Recommended Articles
Page :