Link to home
Start Free TrialLog in
Avatar of surfsideinternet
surfsideinternet

asked on

Javascript Table Sort Problem - Column Hypothetical Results

See https://www.experts-exchange.com/questions/24313854/Fix-Javascript-table-sorting-problem.html

Unfortunately, hypothetical results are still sorting alpha instead of numerical.

See http://www.mffais.com/ibm

and click on "Hypothetical Results"

Would like to know what to change to make it sort correctly for that column also.
Avatar of ewest02
ewest02
Flag of United States of America image

I noticed that the sort results differ depending on which column was sorted first, how many times a target column was sorted... I am wondering about the span.getAttribute( "sortdir")  line in the script. You do not have any span tags with that attribute -- you do have "sortarrow"
Avatar of surfsideinternet
surfsideinternet

ASKER

Okay, so how is the best way to correct it?
Avatar of Michel Plungjan
mplungjan,

In the original question, we said why we used this script instead of the original one.

"We would like to us this one to get dynamic alternating colors, nice arrows"
Ok, then you need to take the object sort used in that script to sort your money string

Michel
This one:

function(a,b) {
    aa = parseFloat(a[0].replace(/[^0-9.-]/g,''));
    if (isNaN(aa)) aa = 0;
    bb = parseFloat(b[0].replace(/[^0-9.-]/g,'')); 
    if (isNaN(bb)) bb = 0;
    return aa-bb;
  }

Open in new window

mplungjan,

Not sure what I am suppose to do with that function. The script has several functions depending on type of data. Can you tell me which lines in the script to change and what they should be.

The script is at: http://www.mffais.com/sorttable.js

Thanks
Sorry, I will be offline a lot until monday.
Haha that sounded very nerdy - I meant, I will not be online a lot
Your regex that checks for numeric values does not handle the case that the minus sign can follow the £, $, ¬ ... symbols.   A number of the form -$12.34 will be properly detected as a numeric value. $-12.35 will be detected as a alphabetic string.

  --Eric
Eric,

What should the regex be then?
A related question:  Do you want the minus sign before or after the currency symbol (eg $)?

line 108 in the script:  if (itm.match(/^-?[£$¬Û¢´]\d/))
try
                  if (itm.match(/^-?[£$¬Û¢´]-?\d/))

note that is not a perfect solution.. It will match -$-1 ...you might want another regex to exactly match

 $- cases

                     if (itm.match(/^[£$¬Û¢´]-?\d/))

Here the regex looks for at most 1 minus sign following the currency symbol.




                 
ewset2,

I have made your modifications ( using   if (itm.match(/^-?[£$¬Û¢´]-?\d/)) ) to sorttable and you can use
http://test.mffais.com/123912 to see the effects of your suggestions.

That almost works, but some of the other columns are not working. Example, "Hypothetical Return" with a ending % and sometimes a - for not return or -13.4% for negative return. Anyway to get all the columns on that page to sort correctly?

Thanks

ASKER CERTIFIED SOLUTION
Avatar of ewest02
ewest02
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ewest02,

I would think it would be easier to detect non-numerical (excluding -,%,$). I think the only problem would be if sorting a column of names that have a - or a % as part of the name. I will try your suggestion above
ewest2,

I tried your latest suggestion and all appears to be working great. Thanks.
Thank you, your final suggestion works like a charm.