p5.js | sort() function

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
               

chevron_right


Output:

Example 2: This example uses sort() function to sort the array elements.

filter_none

edit
close

play_arrow

link
brightness_4
code

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);           

chevron_right


Output:

Example 3: This example uses sort() function to sort the array elements.

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
               

chevron_right


Output:

Reference: https://p5js.org/reference/#/p5/sort



My Personal Notes arrow_drop_up

Recommended Posts:



    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.