Solved

table column sorting problem

Posted on 2009-07-13
5
225 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
[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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Boolean 13 48
Toggle a div with a button and timer with jQuery 4 25
close window in firefox 3 37
How to send a javascript array via AJAX (jquery)? 3 40
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 …
This article discusses how to implement server side field validation and display customized error messages to the client.
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

739 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