Can we have the multi currency sorting

Can we have the multi currency sorting

ManiPSManiPS Posts: 28Questions: 12Answers: 0

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

Answers

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,771

    You can use columns.order to set the order preference of the table. You can use orderFixed if you want to always have those columns affect the ordering.

    Kevin

  • ManiPSManiPS Posts: 28Questions: 12Answers: 0
    edited May 2019

    @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

  • glimpsed_chaosglimpsed_chaos Posts: 113Questions: 22Answers: 4
    edited May 2019

    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.

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,771
    Answer ✓

    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. Use columns.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:

    $ 10 | $ | 10
    -------------
    € 30 | € | 30
    -------------
    

    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:

    $ 10 | 1 | 10
    -------------
    € 30 | 2 | 30
    -------------
    

    The last two columns are hidden but you can use the order and orderFixed 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

  • ManiPSManiPS Posts: 28Questions: 12Answers: 0

    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

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,771

    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 of columns.orderData.

    Kevin

  • allanallan Posts: 61,732Questions: 1Answers: 10,110 Site admin

    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

  • ManiPSManiPS Posts: 28Questions: 12Answers: 0

    @kthorngren

    Thank you so much. It is working fine.

This discussion has been closed.