JavaScript 2022

Term 3 Wednesday

May - July 2022

This site will be updated live during the sessions

Week 3 (b) Array Sort Method

		
001let pets = ['goldfish','dog','cat','parrot','monkey'];
		
002
		
003pets.sort(); // sorts the array alabetically
		
004
		
005console.log(pets.toString());
		
006
		
007let nums = [1,2,3,10,11,12,20,22];
		
008// sort
		
009nums.sort();
		
010//log
		
011console.log(nums.toString()); // sort is alphabetical!!!
		
012
		
013//sort with a function sent in as an argument
		
014nums.sort(function(a, b){return a-b});
		
015//log
		
016console.log(nums.toString()); // sort is numerical!!!
		
017
		
018/*
		
019
		
020
		
021
		
022
		
023
		
024		
		
025
		
026		
		
027
		
028		
		
029//include a function as an argument in the sort method
		
030		
		
031// to sort numerically 
		
032		
		
033console.log(nums.sort(function(a, b){return a-b}).toString()); 
		
034*/