Sorting HTML Table based on a column (Using JavaScript)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Raghuram Banda

    Sorting HTML Table based on a column (Using JavaScript)

    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

  • Lasse Reichstein Nielsen

    #2
    Re: Sorting HTML Table based on a column (Using JavaScript)

    Raghuram Banda <raghuram.banda @nokia.sify.net > writes:
    [color=blue]
    > 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)[/color]

    "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(no de) {
    if (node.nodeType == 3) { // text node;
    return node.nodeValue;
    }
    if (node.nodeType == 1) { // node
    var res = "";
    for (var cnode = node.firstChild e;cnode;cnode=c node.nextSiblin g) {
    res += getInnerText(cn ode);
    }
    return res;
    }
    return ""; // not content node
    }
    [color=blue]
    > 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.[/color]

    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
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Da Costa Gomez

      #3
      Re: Sorting HTML Table based on a column (Using JavaScript)

      Raghuram Banda wrote:[color=blue]
      > 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
      >[/color]
      Have a look at the following link:



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

      Cheers,
      Fermin DCG

      Comment

      • Dr John Stockton

        #4
        Re: Sorting HTML Table based on a column (Using JavaScript)

        JRS: In article <vfry74u9.fsf@h otpop.com>, seen in
        news:comp.lang. javascript, Lasse Reichstein Nielsen <lrn@hotpop.com >
        posted at Fri, 12 Sep 2003 11:07:10 :-[color=blue]
        >Raghuram Banda <raghuram.banda @nokia.sify.net > writes:[/color]
        [color=blue][color=green]
        >> 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).
        >> ...[/color]
        > ...
        >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)?[/color]

        But where is local <g> ?
        [color=blue]
        >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.[/color]

        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 .

        --
        © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
        <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

        Comment

        Working...