Sorting HTML Table based on a column (Using JavaScript)

R

Raghuram Banda

Hi,

I was strucked in a problem, basically I'm working on Sortable Table and
the table is created using HTML Tags and one of the fields in the table
contain Date in unix format (something like Tue Sep 02 01:00:00 2003). I
wrote the script using DOM. I collected innerHTML of a particular cell
and checked whether it a string or not (using isNaN) and based on that I
sorted the rows based on a column. Coming to this date field my sort
technique is not working becauce it treats the cell value as a string
and sorts the table based string sorting. Can any one help me in this
regard.

Thanks in advance
 
L

Lasse Reichstein Nielsen

Raghuram Banda said:
I was strucked in a problem, basically I'm working on Sortable Table
and the table is created using HTML Tags and one of the fields in the
table contain Date in unix format (something like Tue Sep 02 01:00:00
2003). I wrote the script using DOM. I collected innerHTML of a
particular cell and checked whether it a string or not (using isNaN)

"a number or not" I assume. They are all strings.

Using DOM and (non DOM) "innerHTML" is somewhat at a conflict. You can
extract the contents of the cells with DOM as well.

function getInnerText(node) {
if (node.nodeType == 3) { // text node;
return node.nodeValue;
}
if (node.nodeType == 1) { // node
var res = "";
for (var cnode = node.firstChilde;cnode;cnode=cnode.nextSibling) {
res += getInnerText(cnode);
}
return res;
}
return ""; // not content node
}
and based on that I sorted the rows based on a column. Coming to this
date field my sort technique is not working becauce it treats the cell
value as a string and sorts the table based string sorting. Can any
one help me in this regard.

The first question you need to answer, is:
What do you want to happen?

Sure, we can convert a date to a number, if we know it is a date. What
number do you want?

Should the above become 20030902 (optionally adding 010000 for the
time, as a direct coding of the date)?
Or 1062457200000 (the value of the corresponding Date object in local time)?
Or 1062464400000 (the value if it is UTC time)?

The obvious answer to your question is: Recognize it as a date and treat
it as such.

/L
 
D

Da Costa Gomez

Raghuram said:
Hi,

I was strucked in a problem, basically I'm working on Sortable Table and
the table is created using HTML Tags and one of the fields in the table
contain Date in unix format (something like Tue Sep 02 01:00:00 2003). I
wrote the script using DOM. I collected innerHTML of a particular cell
and checked whether it a string or not (using isNaN) and based on that I
sorted the rows based on a column. Coming to this date field my sort
technique is not working becauce it treats the cell value as a string
and sorts the table based string sorting. Can any one help me in this
regard.

Thanks in advance
Have a look at the following link:

http://webfx.eae.net/dhtml/sortabletable/sortabletable.html

I believe this also deals with dates in a non-string manner.

Cheers,
Fermin DCG
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
...
Should the above become 20030902 (optionally adding 010000 for the
time, as a direct coding of the date)?
Or 1062457200000 (the value of the corresponding Date object in local time)?

But where is local said:
Or 1062464400000 (the value if it is UTC time)?

The obvious answer to your question is: Recognize it as a date and treat
it as such.

Since the sort method works on strings, it may be worth adding that the
milliseconds counts can be used for dates in ranges such as (1 to 10 Gs)
2001-09-09 Sun 01:46:40 GMT to 2286-11-20 Sat 17:46:39 GMT.

If Date Objects are to be sorted by default comparison, all the Fridays
cone first ...

I believe function Fn(D1, D2) { return D1 - D2 } and .sort(Fn)
will sort chronologically.
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top