473,769 Members | 7,923 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

array sorting with blank spaces: do IE and Mozilla handle this differently?


Hello,

I am at wit's end with an array sorting problem. I have a
simple table-sorting function which must, at times, sort on columns that
include entries with nothing but a space (@nbsp;). I want all of the
spaces to be put in the first slots of the array. IE 6 does this. But
Firefox 0.9.1 doesn't, and I don't know why.
I have not been able to reproduce it in very simple form (which
is itself a puzzle). But example code is available at
http://cgi.stanford.edu/~bullock2/dirlist.pl?threat: at this page,
sorting on the "Size" column works as I want in IE, but not in Firefox.
I suspect that the problem lies with my code and not with the browsers.
For what it's worth, here is the sorting function:

function RowCompareNumbe rs(a, b) {
if (a.value==" " & b.value==" ") return 0;
else if (a.value==" ") return -1;
else if (b.value==" ") return 1;
else {
var aVal = parseInt(a.valu e);
var bVal = parseInt(b.valu e);
return (aVal - bVal);
}
}

(I know this can be made much more concise, but for the moment,
I'm not worried about it.)

There is also getInnerText() function which retrieves the text
from the table cells; it may be the culprit.

Many thanks,
--John
Jul 23 '05 #1
4 2545

By the way, there is a long and useful thread from mid-February on
browser discrepancies in array sorting, but I couldn't see that it
helped for this particular problem.
"John Bullock" <jo**********@s tanford.edu> wrote in message
news:cc******** **@news.Stanfor d.EDU...

Hello,

I am at wit's end with an array sorting problem. I have a
simple table-sorting function which must, at times, sort on columns that include entries with nothing but a space (@nbsp;). I want all of the
spaces to be put in the first slots of the array. IE 6 does this. But Firefox 0.9.1 doesn't, and I don't know why.
I have not been able to reproduce it in very simple form (which is itself a puzzle). But example code is available at
http://cgi.stanford.edu/~bullock2/dirlist.pl?threat: at this page,
sorting on the "Size" column works as I want in IE, but not in Firefox. I suspect that the problem lies with my code and not with the browsers. For what it's worth, here is the sorting function:

function RowCompareNumbe rs(a, b) {
if (a.value==" " & b.value==" ") return 0;
else if (a.value==" ") return -1;
else if (b.value==" ") return 1;
else {
var aVal = parseInt(a.valu e);
var bVal = parseInt(b.valu e);
return (aVal - bVal);
}
}

(I know this can be made much more concise, but for the moment, I'm not worried about it.)

There is also getInnerText() function which retrieves the text
from the table cells; it may be the culprit.

Many thanks,
--John


Jul 23 '05 #2

I found an array sorting function that produced identical (and
desirable) behavior in both IE 6 and Firefox 0.9.1. The question
becomes: why do the two functions produce different behavior in FireFox
but not in IE?

The function that works for both browsers:

function RowCompareNumbe rsX(a, b) {
var aVal = parseInt(a.valu e);
var bVal = parseInt(b.valu e);
if (isNaN(aVal) && isNaN(bVal)) return 0;
else if (isNaN(parseInt (a.value))) return -1;
else if (isNaN(parseInt (b.value))) return 1;
else return (aVal - bVal);
}

The function that produces a different sort (from the one above) in
Firefox, but the same in IE:

function RowCompareNumbe rs(a, b) {
var aVal = parseInt(a.valu e);
var bVal = parseInt(b.valu e);
if (a.value==" " & b.value==" ") return 0;
else if (a.value==" ") return -1;
else if (b.value==" ") return 1;
else return (aVal - bVal);
}

Thanks again,
--John

"John Bullock" <jo**********@s tanford.edu> wrote in message
news:cc******** **@news.Stanfor d.EDU...

Hello,

I am at wit's end with an array sorting problem. I have a
simple table-sorting function which must, at times, sort on columns that include entries with nothing but a space (@nbsp;). I want all of the
spaces to be put in the first slots of the array. IE 6 does this. But Firefox 0.9.1 doesn't, and I don't know why.
I have not been able to reproduce it in very simple form (which is itself a puzzle). But example code is available at
http://cgi.stanford.edu/~bullock2/dirlist.pl?threat: at this page,
sorting on the "Size" column works as I want in IE, but not in Firefox. I suspect that the problem lies with my code and not with the browsers. For what it's worth, here is the sorting function:

function RowCompareNumbe rs(a, b) {
if (a.value==" " & b.value==" ") return 0;
else if (a.value==" ") return -1;
else if (b.value==" ") return 1;
else {
var aVal = parseInt(a.valu e);
var bVal = parseInt(b.valu e);
return (aVal - bVal);
}
}

(I know this can be made much more concise, but for the moment, I'm not worried about it.)

There is also getInnerText() function which retrieves the text
from the table cells; it may be the culprit.

Many thanks,
--John


Jul 23 '05 #3
"John Bullock" <jo**********@s tanford.edu> writes:
The question becomes: why do the two functions produce different
behavior in FireFox but not in IE? The function that produces a different sort (from the one above) in
Firefox, but the same in IE:

function RowCompareNumbe rs(a, b) {
var aVal = parseInt(a.valu e);
This should be
var aVal = parseInt(a.valu e, 10);

The original version (without the base as argument to parseInt) will
read "012" as 10 in some browsers (read as octal because of initial
0).
var bVal = parseInt(b.valu e);
if (a.value==" " & b.value==" ") return 0;

^
^ that is bitwise and.

You compare the valus to strings of length 1 contatining only one
space. I'll habe to assume that you know that the value won't be the
empty string or two spaces or something else. However, my guess is
that this is the problem you are having.

Bitwise and turns both arguments into 32 bit integers and then
performs the bitwise and on these. In this case, it shouldn't matter,
since booleans turned into numbers become 0 and 1 (for false and true)
and bitwise and gives the same result as logical and.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #4
John Bullock wrote:
Hello,

I am at wit's end with an array sorting problem. I have a
simple table-sorting function which must, at times, sort on columns that
include entries with nothing but a space (@nbsp;). I want all of the
spaces to be put in the first slots of the array. IE 6 does this. But
Firefox 0.9.1 doesn't, and I don't know why.
I have not been able to reproduce it in very simple form (which
is itself a puzzle). But example code is available at
http://cgi.stanford.edu/~bullock2/dirlist.pl?threat: at this page,
sorting on the "Size" column works as I want in IE, but not in Firefox.
I suspect that the problem lies with my code and not with the browsers.
For what it's worth, here is the sorting function:

function RowCompareNumbe rs(a, b) {
if (a.value==" " & b.value==" ") return 0;
You're using a bitwise & here, not a logical one. Although it may be working
as expected most of the time, I'm sure it's not what you intended.
else if (a.value==" ") return -1;
else if (b.value==" ") return 1;
else {
var aVal = parseInt(a.valu e);
var bVal = parseInt(b.valu e);
return (aVal - bVal);
}
}
Your comparator is doing way more work then is necessary. You just need to
normalize any non-numeric data to numbers, in this case, zero (because you
want them first in the array). The way to achieve this is by converting each
value to a number, then changing anything that isNaN into 0. Here is a
sample that works in IE6SP1, Netscape 4.78, Opera 7.5.2 and Firefox 0.9.1:

// sample data
var myArray = [
{value:123},
{value:'&nbsp;' },
{value:234},
{value:' '},
{value:'&nbsp;' },
{value:345},
{value:' '}
];
// sort using custom comparator
myArray.sort(Ro wCompareNumbers );

// test output
for (var i = 0; i < myArray.length; i++) {
document.write( '[' + myArray[i].value + ']<br>');
}

// comparator
function RowCompareNumbe rs(a, b) {
var aa = +a.value || 0;
var bb = +b.value || 0;
return (aa - bb);
}
(I know this can be made much more concise, but for the moment,
I'm not worried about it.)
Cleaning up the comparator to generate reliable, consistent sorted lists
with sample data is the first step in getting this working, so you should be
worried about it. You're doing parseInt() without a second parameter, so if
any of your data contains leading zeros, it'll be converted to hexadecimal,
which might contain letters, which would cause an error when you attempt to
"return (aVal - bVal);".
There is also getInnerText() function which retrieves the text
from the table cells; it may be the culprit.


Well then, what I'd suggest you do is populate an array with the data you
are getting from the table cells to see what that is exactly (by dumping it
to a <textarea> or alert()ing it for example.

Also, Mozilla doesn't support the "innerText" property, so I'm hoping your
"getInnerText() " function actually uses "innerHTML" (although it should be
using nodeValue). Even if it does use "innerHTML" , the representation of
"innerHTML" differs from browser to browser (see <url:
http://jibbering.com/faq/faq_notes/a....html#innHTest />) so what
IE is retrieving from the table cell might be completely different from what
Mozilla is retrieving.

Check the data you are trying to sort before checking anything else
(although you can use my comparator). Garbage in, garbage out as they say.

--
Grant Wagner <gw*****@agrico reunited.com>
comp.lang.javas cript FAQ - http://jibbering.com/faq
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
6696
by: Jean-Marc Molina | last post by:
Hello, I can't find a way to execute a Windows application, whose directory path contains blank spaces, from a PHP script. I also wonder if the problem happens under Linux and other OS. Working dir : "C:\test copy" "copy.php" PHP script : <?php
11
3267
by: Dr John Stockton | last post by:
Q1 : Given an array such as might have been generated by var A = is there a highly effective way of reducing it to - i.e. removing the undefineds and shifting the rest down? A.sort().slice(0,n) // would do it, but sorts; and the number
3
3995
by: gambler | last post by:
let's say you have: var games = new Array(); games = new GAME(gameNum, rotNum1, rotNum2, ... ); ( so a sparsley populate array which enables me to locate a game usin the game number without having to implement a "search" function on th games array
21
3222
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a comma delimited list of values var jsData = new Array(); jsData = {lib: "#field...
3
2950
by: Chris Sharman | last post by:
Are spaces allowed in names ? Eg <input name="my field" type="text" value="my data"> The html4 dtd seems to say this is cdata, which allows embedded single spaces, but say agents may trim leading & trailing space, and should replace any whitespace character(s) with a single space. It then goes on to give further constraints "not expressed by the DTD", restricting id & name to a letter, followed by alphanumerics, hyphen, underscore,...
3
4232
by: yehaimanish | last post by:
I am developing an application by which to parse the content from the access_log and insert it into the database. Since each row is an different entry, I am using file() to get the contents into an array and manipulate each row by foreach(...) and insert/update in the database accordingly. If the file is small, it works well. If the file is large (say > 5mb), it generates memory allocation error. However I came to know that the allowed...
30
2948
by: josh | last post by:
Hi all, what does it meaning that strange sintax (look at the object :) ? if I have i.e. array.length I can use array. and is it IE/Firefox compatible??
3
2259
by: gemguy | last post by:
hi, I have a issue with an array values... Im having values repeatedly stored in an array and i want to delete those repeated values using javascript. * I tried it by storing in an array and sorting it using the blank spaces * I used regexp for it replace it an order but i cant find a solution... anybody have a nice result for it... help me pls... I expect it with an example with code... gemguy
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10049
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.