Declaring arrays
Reversing array
var myarray=["Bob", "Bully", "Amy"];Sorting arrays
- It’s done by array.sort()
- It’s a method that’s probably as much misunderstood as it is underestimated
- Sort’s in ascending order. no direct way to sort in descending order
var myarray=["Bob", "Bully", "Amy"]; myarray.sort(); // Array now becomes ["Amy", "Bob", "Bully"]
- Sort’s in lexicographical (aka alphabetical) order. Numeric sort not allowed
var myarray=[7, 40, 300]; myarray.sort();//Array now becomes [300,40,7] //Numerically 7<40<300 but lexicographically 7>40>300. //So we shouldn't use it for numeric sort. (array.reverse() may be used)Customized Sorting of arrays
- Customized sort with a function argument to array.sort()
- The function should have 2 arguments
- The function should return integer value based on following conditions
- Less than 0(negative): Sort “a” to be a lower index than “b”.
- Zero: “a” and “b” should be considered equal, and no sorting performed.
- Greater than 0(positive): Sort “b” to be a lower index than “a”.
function sortfunction(a, b) { //Compare "a" and "b" in some fashion, and return -1, 0, or 1 } var myarray=["Bob", "Bully", "Amy"]; myarray.sort(sortfunction); //array will be sorted with fashion used.
- Sorting an array in numerical ascending
var myarray=[25, 8, 7, 41]; myarray.sort(function(a,b) { return a - b; }); //Array is [7, 8, 25, 41]
- Sorting an array in numerical descending
var myarray=[25, 8, 7, 41]; myarray.sort(function(a,b){ return b - a; }); //Array is [41, 25, 8, 7]
- Shuffle array by Sorting an array randomly
var myarray=[25, 8, 7, 41]; myarray.sort(function() { return 0.5 - Math.random(); }); //Array is now scrambled
Reversing array
- It’s done by array.reverse()
- Will change the original array itself
- if n is no.of.objects in array. Then object at xth position will be swapped with (n-1)th object.
var myarray=["Bob", "Bully", "Amy"]; myarray.reverse(); //Array now becomes ["Bully", "Bob", "Amy"]Sort array alphabetically in descending order
- Can be done with usage of sort and then reverse.
var myarray=["Bob", "Bully", "Amy"] myarray.sort(); myarray.reverse(); //Array now becomes ["Bully", "Bob", "Amy"]Stack
- A stack follows the Last-In First-Out (LIFO) paradigm
- An item added last will be removed first.
- The Array class has 2 methods that provide stack functionality. they are
push()
andpop()
.- push() appends an item to the end of the array
- pop() removes and returns the last item in the array
var stack = []; stack.push(2); // stack is now [2] stack.push(5); // stack is now [2, 5] var i = stack.pop(); // stack is now [2] alert(i); // displays 5Queue
- A queue follows the First-In First-Out (FIFO) paradigm.
- The first item added will be the first item removed.
- The Array class has 2 methods that provide queue functionality. they are
push()
andshift()
.- push() inserts the passed argument at the end of the array.
- shift() removes and returns the first item.
var queue = []; queue.push(2); // queue is now [2] queue.push(5); // queue is now [2, 5] var i = queue.shift(); // queue is now [5]; alert(i); // displays 2For Queue and Stack these function names may be confusing so we can always create aliases with our own names as below.
var queue = []; queue.add = queue.push; queue.remove = queue.shift; queue.add(1); var i = queue.remove(); alert(i);