Table sorts question

B

BRIAN

I have been trying to use a couple of javascripts to sort a table by
clicking on column headings.
Sorttable.js and tablesort.js which I found on the web...

I am encountering one problem.
In my table, the content of the first cell in each row contains a link
to another location.

When my table is sorted the displayed table appears correct(sorted)
but when I click on the link in the first column it goes to the link
which was there before the sort took place.

When I view source of the page, it always appears as my original html
page.

Obviously I am a javascript beginner(a stretch at that) so I am having
a hard time understanding what is happening.

It looks to me like the screen display is being changed/sorted, but
the actual html in my html file is not and the click is taking the
values from the source file..

Am I correct?

TIA

Brian
 
L

Lasse Reichstein Nielsen

BRIAN said:
I have been trying to use a couple of javascripts to sort a table by
clicking on column headings.
Sorttable.js and tablesort.js which I found on the web...

Could be mine :)
I am encountering one problem.
In my table, the content of the first cell in each row contains a link
to another location.

When my table is sorted the displayed table appears correct(sorted)
but when I click on the link in the first column it goes to the link
which was there before the sort took place.

Highly curious, and sounds mostly like a bug.
Can you show us the page (give a link, or make a small example
file with lines no longer than 72 characters and post it here).
Also specify which browser and version you use.
Obviously I am a javascript beginner(a stretch at that) so I am having
a hard time understanding what is happening.

I'm not, and so do I :)
It looks to me like the screen display is being changed/sorted, but
the actual html in my html file is not and the click is taking the
values from the source file..

The View Source always show the original source, not the current document
structure. I sometimes use this bookmarklet to show the value of innerHTML:

javascript:(document.documentElement||document.body).innerHTML.replace(/&/g,"&amp;").replace(/</g,"&lt;")

It's not pretty, but it shows me what I need. In Mozilla/Netscape 7
you can also use the DOM Inspector to see the current tree structure.

/L
 
A

aboycalled3

Lasse said:
Could be mine :)
<URL:http://www.infimum.dk/privat/sorttable.html>
It was made as an example for someone in this group, IIRC.




Highly curious, and sounds mostly like a bug.
Can you show us the page (give a link, or make a small example
file with lines no longer than 72 characters and post it here).
Also specify which browser and version you use.




I'm not, and so do I :)




The View Source always show the original source, not the current document
structure. I sometimes use this bookmarklet to show the value of innerHTML:

javascript:(document.documentElement||document.body).innerHTML.replace(/&/g,"&amp;").replace(/</g,"&lt;")

It's not pretty, but it shows me what I need. In Mozilla/Netscape 7
you can also use the DOM Inspector to see the current tree structure.

/L

Like the original poster, I am a javascript beginner. I, too, have been
working with Stuart Langridge's sorttable.js:

http://www.kryogenix.org/code/browser/sorttable/

I like it because it's very simple to implement. Unfortunately, I'm
having two problems with it.

1. He created a class called "sortbottom" for rows which contain data
such as totals so that they'll always be placed in the bottom row. But
he didn't create a class called "sorttop" or "static" for those rows
that aren't the top row but should remain in place rather than getting
sorted. It's easier to show than explain:

http://www.elementarydesign.com/BaupostSummaryCompact.html

BTW, I got much of the (x)html/css from Elderweb.com's excellent "When
You Absolutely, Positively Need a Table." It's valid code and very
useful... but it would be more useful if I could get the sorttable.js to
support the markup.

2. Javascript seems to choke when it comes to sorting numbers with
recursive commas. I've seen some work in this area by a frequent poster

http://www.merlyn.demon.co.uk/js-maths.htm#RComma

but I lack the ability to take his work and make it work in sorttable.js

I've been working for a week or so on a number of ideas, none of them
even close to successfully. Any help would be greatly appreciated.

Thanks,

abc3
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
2. Javascript seems to choke when it comes to sorting numbers with
recursive commas. I've seen some work in this area by a frequent poster

http://www.merlyn.demon.co.uk/js-maths.htm#RComma

but I lack the ability to take his work and make it work in sorttable.js


You need to be aware of the importance of the difference in javascript
between a Number and a String.

A Number is a 64-bit IEEE floating-point double, and can be used
arithmetically.

A String is an ordered arrangement of characters, and can be input or
output.

There is automatic conversion between Number and String, which quite
often does what is wanted.

If something contains commas, it cannot be a Number and AFAICS it must
be a String.

Using the default sorting functions :

Number sorts use the numerical value of the Number, sorting into
numerical order.

String sorts use lexical alphanumerical order, starting from the left.

Thus 3 < 2e4, but "3" > "2e4".

To get numerical-magnitude-order by default-sorting Strings, the strings
must be such that the numbers are correspondingly-formatted and the
"ones" are all in the same place; as it happens, it does not matter
whether the shorter numbers are padded with space or zero, provided that
all use the same. They must also not be signed numbers (generally).

You can define any comparison function you like for sorting; you could
even sort a list of numbers into alphabetical order after translation
into Danish, if you could count in Danish words.

Really, commas should only be used when numbers are presented for human
reading; they add no information.
 
A

aboycalled3

Dr said:
JRS: In article <[email protected]>, seen in




You need to be aware of the importance of the difference in javascript
between a Number and a String.

A Number is a 64-bit IEEE floating-point double, and can be used
arithmetically.

A String is an ordered arrangement of characters, and can be input or
output.

There is automatic conversion between Number and String, which quite
often does what is wanted.

If something contains commas, it cannot be a Number and AFAICS it must
be a String.

Using the default sorting functions :

Number sorts use the numerical value of the Number, sorting into
numerical order.

String sorts use lexical alphanumerical order, starting from the left.

Thus 3 < 2e4, but "3" > "2e4".

To get numerical-magnitude-order by default-sorting Strings, the strings
must be such that the numbers are correspondingly-formatted and the
"ones" are all in the same place; as it happens, it does not matter
whether the shorter numbers are padded with space or zero, provided that
all use the same. They must also not be signed numbers (generally).

You can define any comparison function you like for sorting; you could
even sort a list of numbers into alphabetical order after translation
into Danish, if you could count in Danish words.

Really, commas should only be used when numbers are presented for human
reading; they add no information.

Thank you, Dr Stockton, both for the website and for following up with
the above post. I have a much better sense of the philosophy than I
would without your assistance. But I'm still not sure how to apply your
suggestions.

1. Should I represent numbers like 10,234,567,890 as 10234567890? It
would be trivial in the spreadsheet I use to prepare the data. But while
I realize the commas don't add in any information, the end-product is
intended for human reading and I have a hard time reading the numbers
withouth them. I'm not sure I'd be comfortable with that solution.

2. Should I idenify numbers which use recursive commas, strip the
commas, sort them, and then reinsert the commas?

Sorttable.js works out types for data:

<code snippet>

// Work out a type for the column
if (table.rows.length <= 1) return;
var itm = ts_getInnerText(table.rows[1].cells[column]);
sortfn = ts_sort_caseinsensitive;
if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d\d\d$/)) sortfn = ts_sort_date;
if (itm.match(/^\d\d[\/-]\d\d[\/-]\d\d$/)) sortfn = ts_sort_date;
if (itm.match(/^[£$]/)) sortfn = ts_sort_currency;
if (itm.match(/^\s*-?[\d\.]+\%?\s*$/)) sortfn = ts_sort_numeric;

</code snippet>

Would it make sense to add in a type for numbers with recursive commas,
e.g.,

if (itm.match(/^(.*\s)?([-+\u00A3\u20AC]?\d+)(\d{3}\b)/)) sortfn =
ts_sort_recursive_comma;

(pattern matching stolen directly from function RComma(S) at
http://www.merlyn.demon.co.uk/js-maths.htm#RComma)

and then strip the commas, sort the data, and add the commas back in
(which I can suggest but don't know how to accomplish -- has anyone
already written this function)?

3. Or is there a better way?
 
B

bagbourne

Re String/Number comparisons.

Why not create the table in Javascript from a 2D Array of Objects?

Use the DOM to create a table, and then sort using the Array rather than
Strings from the table cells.

I have a working version at work. It sorts correctly on Numbers,
Strings, and Dates.

Sort the Array, then reload the table.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in

BTW, 'recursive' applies only to the method of generation. The
resulting string is just as any other method would give.

Please do not quote more than is necessary.

1. Should I represent numbers like 10,234,567,890 as 10234567890? It
would be trivial in the spreadsheet I use to prepare the data. But while
I realize the commas don't add in any information, the end-product is
intended for human reading and I have a hard time reading the numbers
withouth them. I'm not sure I'd be comfortable with that solution.

The beginning of this thread is not at present available to me; I do not
see how the spreadsheet relates to the javascript. But AIUI
spreadsheets also store numbers as standard binaries, and strings as
rows of characters. If your sheet shows commas, that is a display
option.

You could have numbers shown with commas in one column, for reading, and
the same numbers without commas, for exporting.

2. Should I idenify numbers which use recursive commas, strip the
commas, sort them, and then reinsert the commas?

No. If they have commas, they are not numbers.

Would it make sense to add in a type for numbers with recursive commas,

You could do, but ISTM that you should know what you are sorting. A
date dd/mm/yyyy should probably not be sorted in the same way as a part
number kk/qq/aaaa.

3. Or is there a better way?

Preferably, sort on the binary numbers themselves.
Otherwise, sort on any string form, provided that it had been padded so
that the twos of 2, 12, 112, 1112, etc. are all at the same distance
from the left. Other constant characters make no difference; the
essential is that an alphanumeric short should give also numeric order.
 
L

Lasse Reichstein Nielsen

Dr John Stockton said:
You can define any comparison function you like for sorting; you could
even sort a list of numbers into alphabetical order after translation
into Danish, if you could count in Danish words.

For no apparent reason, I have made a converter from numbers to Danish:
<URL:http://www.infimum.dk/privat/toDanishNumbers.html>
(No work has been put into making it work in less than standard compliant
browsers, and it uses both "eval" and "toFixed" :)

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top