Table sorts question

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

    Table sorts question

    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


  • Lasse Reichstein Nielsen

    #2
    Re: Table sorts question

    BRIAN <bbkNOSPAMassoc iates@rogers.co m> writes:
    [color=blue]
    > 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...[/color]

    Could be mine :)
    <URL:http://www.infimum.dk/privat/sorttable.html>
    It was made as an example for someone in this group, IIRC.
    [color=blue]
    > 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.[/color]

    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.
    [color=blue]
    > Obviously I am a javascript beginner(a stretch at that) so I am having
    > a hard time understanding what is happening.[/color]

    I'm not, and so do I :)
    [color=blue]
    > 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..[/color]

    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:(doc ument.documentE lement||documen t.body).innerHT ML.replace(/&/g,"&amp;").repl ace(/</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
    --
    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

    • aboycalled3

      #3
      Re: Table sorts question

      Lasse Reichstein Nielsen wrote:
      [color=blue]
      > BRIAN <bbkNOSPAMassoc iates@rogers.co m> writes:
      >
      >[color=green]
      >>I have been trying to use a couple of javascripts to sort a table by
      >>clicking on column headings.
      >>Sorttable.j s and tablesort.js which I found on the web...[/color]
      >
      >
      > Could be mine :)
      > <URL:http://www.infimum.dk/privat/sorttable.html>
      > It was made as an example for someone in this group, IIRC.
      >
      >[color=green]
      >>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.[/color]
      >
      >
      > 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.
      >
      >[color=green]
      >>Obviously I am a javascript beginner(a stretch at that) so I am having
      >>a hard time understanding what is happening.[/color]
      >
      >
      > I'm not, and so do I :)
      >
      >[color=green]
      >>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..[/color]
      >
      >
      > 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:(doc ument.documentE lement||documen t.body).innerHT ML.replace(/&/g,"&amp;").repl ace(/</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[/color]

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



      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:

      Shop a wide selection of domains at HugeDomains.com. Find the right domain name today.


      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



      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

      Comment

      • Dr John Stockton

        #4
        Re: Table sorts question

        JRS: In article <YFpCb.642$726. 619@nwrdny02.gn ilink.net>, seen in
        news:comp.lang. javascript, aboycalled3 <aboycalled3@my NOrealSPAMbox.c om>
        posted at Fri, 12 Dec 2003 20:37:12 :-[color=blue]
        >
        >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[/color]


        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.

        --
        © 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> Jsc maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics, links.

        Comment

        • aboycalled3

          #5
          Re: Table sorts question

          Dr John Stockton wrote:
          [color=blue]
          > JRS: In article <YFpCb.642$726. 619@nwrdny02.gn ilink.net>, seen in
          > news:comp.lang. javascript, aboycalled3 <aboycalled3@my NOrealSPAMbox.c om>
          > posted at Fri, 12 Dec 2003 20:37:12 :-
          >[color=green]
          >>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[/color]
          >
          >
          >
          > 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.[/color]

          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.len gth <= 1) return;
          var itm = ts_getInnerText (table.rows[1].cells[column]);
          sortfn = ts_sort_caseins ensitive;
          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_currenc y;
          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_recursi ve_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?


          Comment

          • bagbourne

            #6
            Re: Table sorts question

            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.

            Comment

            • Lasse Reichstein Nielsen

              #7
              Re: Table sorts question

              Dr John Stockton <spam@merlyn.de mon.co.uk> writes:
              [color=blue]
              > 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.[/color]

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

              Comment

              • Dr John Stockton

                #8
                Re: Table sorts question

                JRS: In article <_iICb.3992$w23 .1087@nwrdny02. gnilink.net>, seen in
                news:comp.lang. javascript, aboycalled3 <aboycalled3@my NOrealSPAMbox.c om>
                posted at Sat, 13 Dec 2003 17:49:46 :-
                [color=blue][color=green][color=darkred]
                >>>recursive commas.[/color][/color][/color]

                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.

                [color=blue]
                >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.[/color]

                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.

                [color=blue]
                >2. Should I idenify numbers which use recursive commas, strip the
                >commas, sort them, and then reinsert the commas?[/color]

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

                [color=blue]
                >Would it make sense to add in a type for numbers with recursive commas,[/color]

                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.

                [color=blue]
                >3. Or is there a better way?[/color]

                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.

                --
                © 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> Jsc maths, dates, sources.
                <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics, links.

                Comment

                Working...