Click to See Complete Forum and Search --> : Sorting arrays


nzgrub
11-30-2003, 12:12 PM
Hi I have the following vb array that I want to sort...
(2264,.11/1849,.33/3456,.1/3786,1/)
we have (database results ID, rate of that result/.....
What I want to do is split at "/" sort the arrays descending using the second number.

So I should end up with
(3786,1/1849,.33/2264,.11/3456,.1/)

I have this code but it would only work if my string was single entities like (4,3,6,9)

<script language=JScript runat=server>
function SortVBArray(arrVBArray) {
return arrVBArray.toArray().sort().join('\b');
}
</script>

<%
Function SortArray(arrInput)
SortArray = Split(SortVBArray(arrInput), "/")
End Function
%>

Any help most appreciated.
Thank you, John.

AdamBrill
11-30-2003, 02:16 PM
You might want to ask in the ASP forums since this appears to be an ASP question...

nzgrub
11-30-2003, 10:19 PM
No, it's the following code where the actual sort is done, I believe that this type of sort is far faster than any asp one....

<script language=JScript runat=server>
function SortVBArray(arrVBArray) {
return arrVBArray.toArray().sort().join('\b');
}
</script>

So it is already split at the "/" I need it to sort by the second number in the arrays eg. 2264,.11 sort by the .11 but keep it associated with the 2264
Hope that makes sense.
Thanks again, John.

Gollum
12-01-2003, 04:04 AM
Hi nzgrub,

to sort by some arbitrary ordering, you need to use the version of sort() that takes a comparison function...

function sortBy2ndValue(a,b)
{
var aA = a.split(",");
var aB = b.split(",");

return aA[1] < aB[1];
}
...
return arrVBArray.toArray().sort(sortBy2ndValue).join('\b');

nzgrub
12-01-2003, 09:59 PM
That looks like it might work but I keep getting the same error...

Microsoft JScript runtime error '800a138f'

'undefined' is null or not an object

/default.asp, line 305

Line 305 reads...

var aB = b.split(",");


Any suggestions.
Much apreciated, John.

Gollum
12-02-2003, 02:52 AM
First I'd like to correct a mistake I'd made...
:eek:
The compare function is supposed to return -1, 0 or 1 depending on whether a<b, a=b or a>b (I've grown too used to STL in C++:( )

As to the error you are getting, it can only mean that an element in your array (the last one perhaps) is null or undefined and so the compare function is trying to compare a string with this null or undefined value. Time to recheck the toArray() function code.
Here's a small example that should show how things should go...

<html>
<head>
<script type="text/javascript">

String.prototype.toArray = function()
{
return this.replace(/\(|\/\)/g,"");
}

function sortBy2ndValue(a,b)
{
var aA = a.split(",");
var aB = b.split(",");

if ( aA[1] < aB[1] ) return -1;
if ( aA[1] > aB[1] ) return 1;
return 0;
}

</script>
</head>
<body>
<script>
var aVB = "(2264,.11/1849,.33/3456,.1/3786,1/)";

document.write(aVB);
document.write("<br>");

var aJS = aVB.toArray().split("/");
document.write(aJS);
document.write("<br>");

aJS.sort(sortBy2ndValue);
document.write(aJS);
document.write("<br>");

var aVB2 = "(" + aJS.join("/") + "/)";
document.write(aVB2);
document.write("<br>");
</script>
</body>
</html>