Sorting a table possible?

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

    Sorting a table possible?

    Hi there,

    Is it at all possible to sort the contents of a table (ordinary html
    <table><tr><td> ...</td></tr></table>) using javascript? What I want is to be
    able to select, for example by klicking a button or checking a radio button,
    whether the table shall be sorted by column 1 och 2. Even if the table has
    three or more columns I'm only interested in sorting (increasingly) by first
    or second.

    I would be nice if I did not have to change my table contents (I already
    have it, 87 rows, 9 columns, with lots of links and stuff) into say an
    array, but if that is the only way then maybe I will have to accept that.

    So, any ideas?

    Many thanks in advance,

    MM



  • Michael Winter

    #2
    Re: Sorting a table possible?

    MM wrote on 03 Dec 2003:
    [color=blue]
    > Hi there,
    >
    > Is it at all possible to sort the contents of a table (ordinary
    > html <table><tr><td> ...</td></tr></table>) using javascript?
    > What I want is to be able to select, for example by klicking a
    > button or checking a radio button, whether the table shall be
    > sorted by column 1 och 2. Even if the table has three or more
    > columns I'm only interested in sorting (increasingly) by first
    > or second.
    >
    > I would be nice if I did not have to change my table contents (I
    > already have it, 87 rows, 9 columns, with lots of links and
    > stuff) into say an array, but if that is the only way then maybe
    > I will have to accept that.[/color]

    I think the most efficient way would be to read the table into a
    multidimensiona l array, sort that, then write it back out using DOM
    methods.

    You could do it more or less in-place, but you'd have to do the sort
    manually, as apposed to using the Array.sort() method.

    Of course, this won't work in older browsers.

    Mike

    --
    Michael Winter
    M.Winter@blueyo nder.co.uk.invalid (remove ".invalid" to reply)

    Comment

    • F. Da Costa

      #3
      Re: Sorting a table possible?

      Just have a look here.


      Helped me a lot

      Cheers
      Fermin DCG

      MM wrote:
      [color=blue]
      > Hi there,
      >
      > Is it at all possible to sort the contents of a table (ordinary html
      > <table><tr><td> ...</td></tr></table>) using javascript? What I want is to be
      > able to select, for example by klicking a button or checking a radio button,
      > whether the table shall be sorted by column 1 och 2. Even if the table has
      > three or more columns I'm only interested in sorting (increasingly) by first
      > or second.
      >
      > I would be nice if I did not have to change my table contents (I already
      > have it, 87 rows, 9 columns, with lots of links and stuff) into say an
      > array, but if that is the only way then maybe I will have to accept that.
      >
      > So, any ideas?
      >
      > Many thanks in advance,
      >
      > MM
      >
      >
      >[/color]

      Comment

      Working...