Solved

table column sorting problem

Posted on 2009-07-13
5
217 Views
Last Modified: 2012-05-07
hi,


I have a problem with sorting of a table.

I don't know if one of you has already worked with this table sort. I am using the tablesort of http://tablesorter.com/docs/. It is working, but i having problems sorting columns with alpha-numeric data in it, columns us amount separators etc....


examples:

i am trying to sort for example numbers like "1,107,748.90" but it is not doing it right.
i am getting the following:

amount:

1,107,748.90
1,727.79
10,050.86  

which is not good.
any idea where can i modify to get it right?



i also have a column with ABDA00364 as account number together with account numbers of only numbers. here too it is not sorting right.
i am getting the following:

account number:

510008123
5431100000
5588990000
5892655105
5892656101
630033456
7190057654
80783824  
ABDA00364
ACCO00605  

which is not good.
any idea where can i modify to get it right?
0
Comment
Question by:eaweb
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:SordSord
ID: 24843735
It looks like all you data is being sorted as TEXT rather than NUMERIC. A quick look at the tablesorter docs indicates that you can create a parser that will let you change how sorting is done ( http://tablesorter.com/docs/example-parsers.html ).

For the numbers, the parser would just need to convert the values to numbers (strip commas, use parseFloat() to convert the result a number).

For the accounts, I'm going to guess that front padding with zeros or maybe spaces to a fixed length will give you the results you want.

0
 

Author Comment

by:eaweb
ID: 24848814
do you maybe have a code sample on how to?
0
 
LVL 3

Accepted Solution

by:
SordSord earned 500 total points
ID: 24861837
Note, I haven't actually used the tablesorter tool, but this would be my first guess based on the docs.

I'm assuming you have two columns, account and balance, and that the account numbers are in the account column and the numbers are in the balance column.
// add parser through the tablesorter addParser method 

$.tablesorter.addParser({ 

  // set a unique id 

  id: 'account', 

  is: function(s) { 

    // return false so this parser is not auto detected 

    return false; 

  }, 

  format: function(s) { 

    // format your data for normalization 

    for ( ; s.length < 10; ) {

      s = "0"+s;

    }

    return s;

  }, 

  // set type, either numeric or text 

  type: 'text' 

}); 
 

$.tablesorter.addParser({ 

  // set a unique id 

  id: 'balance', 

  is: function(s) { 

    // return false so this parser is not auto detected 

    return false; 

  }, 

  format: function(s) { 

    // format your data for normalization 

    return parseFloat(s.replace(/,/g, ""));

  }, 

  // set type, either numeric or text 

  type: 'numeric' 

}); 
 

$(function() { 

  $("table").tablesorter({ 

    headers: { 

      1: { 

        sorter:'account' 

      } 

      2: { 

        sorter:'balance' 

      } 

    } 

  }); 

}); 

Open in new window

0
 

Author Comment

by:eaweb
ID: 24862697
hi SordSord,
i am going to test this out.

i also have another table sorting code which i am having problems with. is it possible for you to take a look at it?
see link

http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_24572185.html
0
 

Author Closing Comment

by:eaweb
ID: 31602876
great thank you very very very much
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
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 …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

757 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

22 Experts available now in Live!

Get 1:1 Help Now