Can we have the multi currency sorting
Can we have the multi currency sorting

Lets say,
I have one column which contains multiple currencies such as $, Euro, Rupee..
Can i sort it first by currency and then sort by amount
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
You can use
columns.order
to set the order preference of the table. You can useorderFixed
if you want to always have those columns affect the ordering.Kevin
@kthorngren
I am not getting you.
For example i have below data in column
$ 10, € 30, € 50, $ 8, INR 10, $ 20, INR 30
When i click on sort header, it should render in below order
$ 8, $ 10, $ 20, € 30, € 50, INR 10, INR 30.
It should order by currency and then order by amount.
Please help me out.
Thanks in advance
Click the currency column you want to sort by and the directional order (asc/desc). Then press/hold the shift key and click the amount column to sort.
Should give you the results you want.
If you meant when the data loads than you can initialize and set it to order by multiple columns as a default like @kthorngren said.
I wasn't sure exactly what you had in your column. i assumed one column had the currency and another had the value. This situation is more complex since you want to sort on two values within the same column.
I would look at adding two hidden columns using
columns.visible
. Usecolumns.render
in each column. The render function in one column will extract the currency and the other will extract the value. So you will have something like this:Since the symbols may not sort the way you want you may want to use a numeric value like $ = 1, € = 2, etc, for something like this:
The last two columns are hidden but you can use the
order
andorderFixed
with them.The other alternative I can think of is to create a sorting plugin. Here is a deprecated plugin you may be able to adapt to your specific data structure:
https://datatables.net/plug-ins/sorting/currency
Kevin
https://jsfiddle.net/ypdcsbak/
I have the sample here. please have a look on it.
11,56$ and 269,75$ should come in ascending order. then all other euro should come in order.
I mean currency sorting first then amount sorting
I wasn't expecting that plugin to work. I linked to it to show and example but expected that it would need modification. Thinking about it, using the plugin method might be too complex.
I think the best and easiest option is to use the hidden column technique I described. Sorry, I gave the incorrect order options to use. Actually you will want to use
columns.orderData
. I updated your example to show this:https://jsfiddle.net/tzkvm7gw/
It is using substr() and substring() to extract the currency and numeric values. You may need to use something more specific depending on your data structure. Uncomment the
visible: false
for the last two columns and you will see the magic ofcolumns.orderData
.Kevin
I guess another option would be to get the current exchange rates and transform them all into a single currency for sorting, but display them in their native currencies.
Allan
@kthorngren
Thank you so much. It is working fine.