I am reading some numbers from a local file and then trying to sort the numbers using Javascript. Reading is done with the help of an ActiveXObject and the sorting with the help of javascript.
When I dont use ActiveX the sorting can be done but in the presence of Activex it doesn’t work. Are there any restrictions on using such functions in presence of ActiveX. If someone can please tell me where the code needs to be altered I would be thankful.
Here is the code.
<html>
<script>
var oRequest;
var i;
var numbers=new Array();
var b= new Array();
var j
var k;
var temp;
var temp1;
if(document.all) {
oRequest = new ActiveXObject("Microsoft.XMLHTTP")
}
else {
oRequest = new XMLHttpRequest();
}
oRequest.open("GET", "file:///C:/Test.txt", true);
oRequest.send(null);
numbers= oRequest.responseText.split("\n");
for(i=0;i<10;i++)
{
for(j=0;j<9;j++)
{
if(numbers[j]>numbers[j+1])
{
temp=numbers[j+1];
numbers[j+1]=numbers[j];
numbers[j]=temp;
}
}}
document.write(numbers);
document.write("<br>");
</script>
</html>
The array
numbersdoesn’t contain numbers, it contains strings. When you compare them they are compared as strings, not as numbers.Parse the strings in the array: