Reputation: 809
I'm using the jquery table sorter with success except for my currency column which is in euros.
This is my ASC result (It's only reading the first number?):
€102 €105 €110 €120 €29 €35 €40
How do I make it so that 29 is first and not 102????
Upvotes: 1
Views: 1582
Reputation: 51
This question is old, but I ran into the same trouble, and thought I'd post my solution in case anyone else has the same problem.
I've got columns with various symbols following the decimal numbers, such as u (units), g (grams) and €.
What WTK posted above is correct, however the column count he's posting is NOT correct. He wrote '2' for column 2, but the count starts at 0, so if you want to sort on column 2, you need to write 1.
As I use US-style decimals, I also had to force this in the code. Here's what worked for me:
$('#mainTable').tablesorter({
usNumberFormat: true,
headers: {
// column number 6, with contents such as: 1.04 g
5: {
sorter: "currency"
},
// column number 7, with contents such as: 1.04 u
6: {
sorter: "currency"
},
// column number 9, with contents such as: 1.04 €
8: {
sorter: "currency"
},
// column number 10, with contents such as: 1.04 €
9: {
sorter: "currency"
},
// column number 11, with contents such as: 1.04 €
10: {
sorter: "currency"
}
}
});
Upvotes: 1
Reputation: 16971
Does it work well if you replace the euro sign with a dollar sign?
If so, than most probably your euro sign is provided as html entity instead of regular utf character and therefor tablesorter plugin doesn't know that it has to treat that column values as a currency. Hence the wrong sorting.
If, on the other hand, you have a proper euro sign in front of your currency values then you can try forcing tablesorter to treat given column as a currency. Like this:
// Create a tablesorter interface and set a column parser for the second column.
$('#example-table').tablesorter({ headers: { 2: {sorter:"currency"} } });
Update: check the source code for you tablesorter plugin, take a closer look at this lines:
ts.addParser({
id: "currency",
is: function (s) {
return /^[£$�.]/.test(s);
}, format: function (s) {
return $.tablesorter.formatFloat(s.replace(new RegExp(/[ÂŁ$âŹ]/g), ""));
}, type: "numeric"
});
At line 4 and line 6 you can see a regex - the column is treated as currency if it passes the test at line 4. Maybe (as here in the example above), encoding of the characters in regexp is messed up. To make sure they're ok, replace the them with your euro character.
Upvotes: 0