Home Categories ASP FAQ Tutorial

How can I sort a VB Script array in ways other than by case-sensitive alphanumeric order, like numeric value, length of string, or even randomly?

3.0/5.0 (4 votes total)
Rate:

Richard Lowe


Richard Lowe
FAQ posted by Richard Lowe to the Arrays category ASP Frequently Asked Questions.
Reprinted with author's and ASPFAQs.com permission.
Richard Lowe has written 1 articles for WebKnowHow.
View all articles by Richard Lowe...

Answer: The sort() method of the JScript array can sort an array in a user-defined way, with a little work. (See this: How can I quickly sort a VBScript array? for further explanation of how the JScript array's sort method is integrated with VBScript arrays.)

In order to sort an array in customized ways, 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
%>


So now a VBScript array, when passed to the SortArray function, will be sorted in case-sensitive alphanumeric order. Sorting in a custom way 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's 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 passed to do the sort instead of it's own internal method (case sensitive alphanumeric). Making the below change sorts by length of the elements (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.


<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>


Add commentAdd comment (Comments: 0)  

Advertisement

Partners

Related Resources

Other Resources