P
ROTOTYPE
I'm Not Only The Prototype, I'm Also A Member.
home
▪
stats
▪
search
▪
linkback
▪
about
▪
FAQ
| user: guest,
login
,
register
Array
:
Alphabetically sort an array, with the chosen order (ASC, DESC)
author:
pedrodinis
[+]
,
Submitted: 10.22.01 10a
// the default order is ascending, to have an descending order parse the "desc"... // this will ignore CAPS when sorting Array.prototype.aSort = function (order) { if (order.toLowerCase() == "desc") { this.sort(DESC); } else { this.sort(ASC); } } function ASC (a , b){ if (typeof(a) == "string" or typeof(b) == "string") { a = a.toLowerCase(); b = b.toLowerCase(); } if (a < b){ return -1; } else if (a > b){ return 1; } return 0; } function DESC (a , b){ if (typeof(a) == "string" or typeof(b) == "string") { a = a.toLowerCase(); b = b.toLowerCase(); } if (a > b){ return -1; } else if (a < b){ return 1; } return 0; }
usage
// put this on the 2nd frame stop(); a1 = new Array("Ca", "aa", "Ba", "Ab", "cb", "bb", "c"); a2 = new Array(1, 3, 5, 6, 11, 0); a3 = new Array("1", 3, "5", "6", 12, 0); a1.aSort("DESC"); a2.aSort(); a3.aSort("desc"); trace(a1 + " | " + a2 + " | " + a3);
msg
1
{
icutommy
[+]
, posted: 07.27.02 9p•07.27.02 10p, top
[^]
}
useful but,
array has reverse(),
so "desc" is not necessary,
and i don't care for Boolean, Array, nor anything ...
but for Number
Array.prototype.aSort = function() { var asc = function (a, b) { a = a.toString().toLowerCase(); b = b.toString().toLowerCase(); if (a<b) { return -1; } else if (a>b) { return 1; } return 0; }; this.sort(asc); }; temp=["az", "Bz", "XYZ"]; a1 = ["Ca", "aa", "Ba", "Ab", "cb", "bb", "c",0,1,2,true,false,"fax",temp ]; a1.sort(); trace(a1); a1.aSort(); trace(a1); a1.reverse(); trace(a1);
Add Comment
[+]
›opyleft 2001-2010. Layer51 is: Jaime Prado.
@