post your comment   print   send to a friend
Rate: 100% | Views: 736
Question categories:  ASP

How can I sort a VB Script array other than in case-sensitive alphanumeric order? What I want to do is display another order like numeric value, length of string, or even a random value.

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>

Customer Feedback
Rate: 100% | Views: 736 | Please Rate:  
 
If you have other comments or ideas for future technical tips, please type them here:

Email: (optional)

Comments: (optional)

 Custom Web Design | Domain Name Registration    Back to serch results
Browse the Base
Knowledge Base
Web Design
  Do It Yourself
    ASP
Messages
 

$75 Free Google AdWords

Free $75 Google AdWords when you sign up for WebImage! Target by location, create your own, or let Google create your ads for you. Check out http://www.aplus.net/google.html to see how AdWords works for you.

Private Area
 
Ask
in Private
   
Personal
Folder
 
Related Questions
 
1. How can I display all of the contents of a single-dimension array?
 
2. Does ASP.Net still recognize the global.asa file?
 
3. Is it possible to run client-side .NET code within a browser?
 
4. What happened to date() and time()?
 
5. How do I display data on a web page using arrays instead of Do...While...MoveNext...???
 
Home Browse Search Ask in Private Personal Folder   Help
powered by web hosting 
  Logged as: Guest