|
The sort() method of the JScript array
will sort an array in a user-defined manner, with a little work.
In order to sort an array in a customized
manner, you first have to implement the sort() method of the JScript
array in VBScript. Because JScript and VBScript arrays are
not directly interchangeable, the two functions below take a VBScript
array and pass it to JScript to do the sorting, then pass it back
to VBScript.
<script
language=JScript runat=server>
function SortVBArray(arrVBArray) {
return arrVBArray.toArray().sort().join('\b');
}
</script>
<%
Function SortArray(arrInput)
SortArray = Split(SortVBArray(arrInput),
Chr(8))
End Function
%>
VBScript array, when passed to the
SortArray function, will be sorted in case-sensitive alphanumeric
order. Custom sorting in works like this: The sort() method takes
one parameter which must be a JScript function: sort(MyCustomSortFunction).
The JScript function (MyCustomSortFunction) must be set up to take
two parameters, compare them and return 1 if the first parameter
should come after the second parameter in the sort, -1 if the first
should come before the second and 0 if they are equal
Here is an example of a custom sort
function:
function SortByLength(a, b) {
if (a.length > b.length) {
return 1// a is longer
}
else {
if (a.length < b.length) {
return -1// a is shorter
}
else {
return 0// same size
}
}
// The below returns -1, meaning that
PC is a shorter
// word and comes before Mac on the
list:
Response.Write(SortByLength("PC", "Mac"));
When this function is passed to the
sort() method, the sort method uses the function to sort instead
of using its innate method (case sensitive alphanumeric). Making
the change below sorts by length of element (assuming all can be
evaluated as strings):
<script
language=JScript runat=server>
function SortVBArray(arrVBArray) {
return arrVBArray.toArray().sort(SortByLength).join('\b');
}
</script>
Here
are some pre-written functions for sorting you can plug into your
application. Just pass the function
name to the sort(MyFunction) method/p>
<script
language=JScript runat=server>
// Sorts records in pseudo-random order
function sRandom() {
return ((Math.random()>=0.5) ?
1 : -1);
// Sorts records in ascending, case-insensitive
// alpha-numeric order.
function sNoCaseAsc(a, b) {
return ((a.toUpperCase()>b.toUpperCase())
? 1 : ((a.toUpperCase()<b.toUpperCase()) ? -1 : 0));
// Sorts records in descending, case-insensitive
// alpha-numeric order.
function sNoCaseDsc(a, b) {
return ((a.toUpperCase()>b.toUpperCase())
? -1 : ((a.toUpperCase()<b.toUpperCase()) ? 1 : 0));
// Sorts records in ascending
// numeric value order
function sNumAsc(a, b) {
return ((+a > +b) ? 1 : ((+a < +b)
? -1 : 0));
// Sorts records in descending
// numeric value order
function sNumDsc(a, b) {
return ((+a > +b) ? -1 : ((+a < +b)
? 1 : 0));
// Sort records in ascending
// string length order
function sLenAsc(a, b) {
return ((('' + a).length > ('' +
b).length) ? 1 : ((('' + a).length < ('' + b).length) ? -1 : 0));
// Sort records in descending
// string length order
function sLenDsc(a, b) {
return ((('' + a).length > ('' +
b).length) ? -1 : ((('' + a).length < ('' + b).length) ? 1 : 0));
}
</script>
|