Solved

Javascript Table Sort Problem - Column Hypothetical Results

Posted on 2009-05-19
17
304 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:surfsideinternet
  • 8
  • 5
  • 4
17 Comments
 
LVL 4

Expert Comment

by:ewest02
Comment Utility
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
 

Author Comment

by:surfsideinternet
Comment Utility
Okay, so how is the best way to correct it?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
0
 

Author Comment

by:surfsideinternet
Comment Utility
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
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
Ok, then you need to take the object sort used in that script to sort your money string

Michel
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
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
 

Author Comment

by:surfsideinternet
Comment Utility
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
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
Sorry, I will be offline a lot until monday.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
Haha that sounded very nerdy - I meant, I will not be online a lot
0
 
LVL 4

Expert Comment

by:ewest02
Comment Utility
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
 

Author Comment

by:surfsideinternet
Comment Utility
Eric,

What should the regex be then?
0
 
LVL 4

Expert Comment

by:ewest02
Comment Utility
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
 

Author Comment

by:surfsideinternet
Comment Utility
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
 
LVL 4

Accepted Solution

by:
ewest02 earned 250 total points
Comment Utility
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
 

Author Comment

by:surfsideinternet
Comment Utility
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
 

Author Comment

by:surfsideinternet
Comment Utility
ewest2,

I tried your latest suggestion and all appears to be working great. Thanks.
0
 

Author Closing Comment

by:surfsideinternet
Comment Utility
Thank you, your final suggestion works like a charm.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to dynamically set the form action using jQuery.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now