Thread: Resolved sorting through objects in an array View Single Post
 03-02-2013, 11:32 AM PM User | #24 pssingh1001 New to the CF scene   Join Date: Feb 2013 Location: new delhi Posts: 4 Thanks: 0 Thanked 0 Times in 0 Posts Hi All ! you can solve this queries through inbuilt function... array.sort([compareFunction]) Parameters compareFunction Specifies a function that defines the sort order. If omitted, the array is sorted lexicographically (in dictionary order) according to the string conversion of each element. Description If compareFunction is not supplied, elements are sorted by converting them to strings and comparing strings in lexicographic ("dictionary" or "telephone book," not numerical) order. For example, "80" comes before "9" in lexicographic order, but in a numeric sort 9 comes before 80. If compareFunction is supplied, the array elements are sorted according to the return value of the compare function. If a and b are two elements being compared, then: If compareFunction(a, b) is less than 0, sort a to a lower index than b. If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this. If compareFunction(a, b) is greater than 0, sort b to a lower index than a. compareFunction(a, b) must always returns the same value when given a specific pair of elements a and b as its two arguments. If inconsistent results are returned then the sort order is undefined So, the compare function has the following form: 1 2 3 4 5 6 7 8 9 function compare(a, b) { if (a is less than b by some ordering criterion) return -1; if (a is greater than b by the ordering criterion) return 1; // a must be equal to b return 0; } To compare numbers instead of strings, the compare function can simply subtract b from a: 1 2 3 4 function compareNumbers(a, b) { return a - b; } Some implementations of JavaScript implement a stable sort: the index partial order of a and b does not change if a and b are equal. If a's index was less than b's before sorting, it will be after sorting, no matter how a and b move due to sorting. For sorting strings of languages other than English, use String.localeCompare. This function can compare e, é, è, a, ä, etc. so they appear in the right order. Sort is stable in SpiderMonkey and all Mozilla-based browsers starting with Gecko 1.9 (see bug 224128). The behavior of the sort method changed between JavaScript 1.1 and JavaScript 1.2. In JavaScript 1.1, on some platforms, the sort method does not work. This method works on all platforms for JavaScript 1.2. In JavaScript 1.2, this method no longer converts undefined elements to null; instead it sorts them to the high end of the array. For example, assume you have this script: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 var a = []; a[0] = "Ant"; a[5] = "Zebra"; // assumes a print function is defined function writeArray(x) { for (i = 0; i < x.length; i++) { print(x[i]); if (i < x.length-1) print(", "); } } writeArray(a); a.sort(); print("\n"); writeArray(a); In JavaScript 1.1, JavaScript prints: ant, null, null, null, null, zebra ant, null, null, null, null, zebra In JavaScript 1.2, JavaScript prints: ant, undefined, undefined, undefined, undefined, zebra ant, zebra, undefined, undefined, undefined, undefined The sort() method can be conveniently used with function expressions (and closures): 1 2 3 4 5 var numbers = [4, 2, 5, 1, 3]; numbers.sort(function(a, b) { return a - b; }); print(numbers); regards prem