Solved

# table column sorting problem

Posted on 2009-07-13
226 Views
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
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
• 3
• 2

LVL 3

Expert Comment

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

ID: 24848814
do you maybe have a code sample on how to?
0

LVL 3

Accepted Solution

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
// 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'
});

// 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({
1: {
sorter:'account'
}
2: {
sorter:'balance'
}
}
});
});
``````
0

Author Comment

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?

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

Author Closing Comment

ID: 31602876
great thank you very very very much
0

## Featured Post

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
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
Course of the Month1 day, 20 hours left to enroll