Javascript Table Sort Problem - Column Hypothetical Results

See http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_24313854.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.
surfsideinternetAsked:
Who is Participating?
 
ewest02Commented:
113   if (itm.match(/^-?(\d+[,\.]?)+(E[-+][\d]+)?%?$/)) sortfn = ts_sort_numeric;

It looks to me like this regex does not handle the case that there is white space between the last digit and the % sign. Try

113   if (itm.match(/^-?(\d+[,\.]?)+(E[-+][\d]+)?\s*%?$/)) sortfn = ts_sort_numeric;

to allow any number of white space characters between a digit and the trailing percent sign. I am not sure why there is a "E" at the start of the second grouping.

I am also wondering if your can loosen your criteria for how you detect a numeric value. Might it be simpler to detect a non-numeric value?

  --Eric
0
 
ewest02Commented:
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"
0
 
surfsideinternetAuthor Commented:
Okay, so how is the best way to correct it?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Michel PlungjanIT ExpertCommented:
0
 
surfsideinternetAuthor Commented:
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"
0
 
Michel PlungjanIT ExpertCommented:
Ok, then you need to take the object sort used in that script to sort your money string

Michel
0
 
Michel PlungjanIT ExpertCommented:
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

0
 
surfsideinternetAuthor Commented:
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
0
 
Michel PlungjanIT ExpertCommented:
Sorry, I will be offline a lot until monday.
0
 
Michel PlungjanIT ExpertCommented:
Haha that sounded very nerdy - I meant, I will not be online a lot
0
 
ewest02Commented:
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
0
 
surfsideinternetAuthor Commented:
Eric,

What should the regex be then?
0
 
ewest02Commented:
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.




                 
0
 
surfsideinternetAuthor Commented:
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

0
 
surfsideinternetAuthor Commented:
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
0
 
surfsideinternetAuthor Commented:
ewest2,

I tried your latest suggestion and all appears to be working great. Thanks.
0
 
surfsideinternetAuthor Commented:
Thank you, your final suggestion works like a charm.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.