Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Javascript Table Sort Problem - Column Hypothetical Results

Posted on 2009-05-19
17
Medium Priority
?
325 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
  • 4
17 Comments
 
LVL 4

Expert Comment

by:ewest02
ID: 24428290
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
ID: 24428360
Okay, so how is the best way to correct it?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24428839
0
Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

 

Author Comment

by:surfsideinternet
ID: 24433061
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
ID: 24433073
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
ID: 24433485
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
ID: 24437952
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
ID: 24438332
Sorry, I will be offline a lot until monday.
0
 
LVL 75

Expert Comment

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

Expert Comment

by:ewest02
ID: 24458063
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
ID: 24458168
Eric,

What should the regex be then?
0
 
LVL 4

Expert Comment

by:ewest02
ID: 24458253
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
ID: 24470050
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 1000 total points
ID: 24474679
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
ID: 24477799
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
ID: 24477851
ewest2,

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

Author Closing Comment

by:surfsideinternet
ID: 31583287
Thank you, your final suggestion works like a charm.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

719 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