Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • p5.js-Environment
  • p5.js-Color
  • p5.js-Shape
  • p5.js-Constants and Structure
  • p5.js-DOM and Rendering
  • p5.js-Transform
  • p5.js-Data
  • p5.js-Events
  • p5.js-Image
  • p5.js-IO
  • p5.js-Math
  • p5.js-Typography
  • p5.js-Lights & Camera
  • JavaScript
  • Web Technology
Open In App
Next Article:
PHP sort() Function
Next article icon

p5.js sort() function

Last Updated : 22 Aug, 2023
Comments
Improve
Suggest changes
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

Next Article
PHP sort() Function

K

Kanchan_Ray
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: C/C++ Code 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 rsort() Function
    The rsort() is an inbuilt function in PHP and is used to sort the array in descending order i.e, greatest to smallest. It sorts the actual array and hence changes are reflected in the array itself. The function provides us with 6 sorting types, according to which the array can be sorted. Syntax: rso
    3 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
  • p5.js shuffle() function
    The shuffle() function in p5.js is used to shuffle the order of given array elements. Syntax: shuffle(Array) Parameters: This function accepts a parameter Array whose elements are to be shuffled. Return Value: It returns the shuffled array. Below program illustrates the shuffle() function in p5.js:
    2 min read
  • p5.js reverse() function
    The reverse() function in p5.js is used to reverse the order of the given array element. Syntax: reverse(Array) Parameters: This function accepts a parameter Array whose elements are to be reversed. Return Value: It returns a new reversed array. Below program illustrates the reverse() function in p5
    1 min read
  • p5.js min() function
    The min() function in p5.js is used to get the minimum value from the sequence of numbers. Syntax: min(a, b) or min(arr) Parameters: The min(a, b) function accepts two parameters which are two different number and compared to get minimum value among them. The min(arr) function accepts a single param
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences