Table sorts question

Discussion in 'Javascript' started by BRIAN, Aug 13, 2003.

  1. BRIAN

    BRIAN Guest

    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
    BRIAN, Aug 13, 2003
    #1
    1. Advertising

  2. BRIAN <> writes:

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

    > 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
    --
    Lasse Reichstein Nielsen -
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Aug 13, 2003
    #2
    1. Advertising

  3. BRIAN

    aboycalled3 Guest

    Lasse Reichstein Nielsen wrote:

    > BRIAN <> writes:
    >
    >
    >>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 :)
    > <URL:http://www.infimum.dk/privat/sorttable.html>
    > It was made as an example for someone in this group, IIRC.
    >
    >
    >>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


    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
    aboycalled3, Dec 12, 2003
    #3
  4. JRS: In article <YFpCb.642$>, seen in
    news:comp.lang.javascript, aboycalled3 <>
    posted at Fri, 12 Dec 2003 20:37:12 :-
    >
    >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.

    --
    © 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.demon.co.uk/js-index.htm> Jsc maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics, links.
    Dr John Stockton, Dec 13, 2003
    #4
  5. BRIAN

    aboycalled3 Guest

    Dr John Stockton wrote:

    > JRS: In article <YFpCb.642$>, seen in
    > news:comp.lang.javascript, aboycalled3 <>
    > posted at Fri, 12 Dec 2003 20:37:12 :-
    >
    >>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.


    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?
    aboycalled3, Dec 13, 2003
    #5
  6. BRIAN

    bagbourne Guest

    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.
    bagbourne, Dec 13, 2003
    #6
  7. JRS: In article <_iICb.3992$>, seen in
    news:comp.lang.javascript, aboycalled3 <>
    posted at Sat, 13 Dec 2003 17:49:46 :-

    >>>recursive commas.


    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.

    --
    © 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.demon.co.uk/js-index.htm> Jsc maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics, links.
    Dr John Stockton, Dec 13, 2003
    #7
  8. Dr John Stockton <> writes:

    > 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
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Dec 13, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Srikanth Mandava
    Replies:
    1
    Views:
    378
    Michael Hudson
    Feb 19, 2004
  2. Replies:
    6
    Views:
    372
  3. Ron Adam

    Alphabetical sorts

    Ron Adam, Oct 16, 2006, in forum: Python
    Replies:
    7
    Views:
    452
    Ron Adam
    Oct 17, 2006
  4. CMOS
    Replies:
    1
    Views:
    309
    Jack Klein
    Aug 29, 2006
  5. Jonathan Wood
    Replies:
    2
    Views:
    311
    Jonathan Wood
    Jun 18, 2008
Loading...

Share This Page