Custom sorting: -asc/-desc not called when -pre is present

Custom sorting: -asc/-desc not called when -pre is present

ralphjeralphje Posts: 2Questions: 0Answers: 0

There seems to be an issue with DT 1.10. When a custom sorting method is built with all three sorting-pre, -asc and -desc methods, -asc and -desc are not called anymore. Only the pre-function is called. Moreover, when -pre is not present, -asc and -desc ARE called.

See this fiddle: http://jsfiddle.net/H5cjN/1/

Replies

  • allanallan Posts: 61,687Questions: 1Answers: 10,100 Site admin

    That's not an "issue", that's a feature! The -pre function is used as an optimisation - it calculated the data to be sorted, so it is only ever called once for every cell. Then the built in sorter can simply sort the data without needing to call external functions - a two fold optimisation.

    If you do want the -asc and -desc functions to be called, don't provide -pre.

    Allan

  • ralphjeralphje Posts: 2Questions: 0Answers: 0
    edited May 2014

    I doubt this constitutes as a feature, as the following can be found in the source of DT 1.10:

    Each ordering option can be described by three properties added to this object:

    • {type}-pre - Pre-formatting function
    • {type}-asc - Ascending order function
    • {type}-desc - Descending order function

    All three can be used together, only {type}-pre or only {type}-asc and {type}-desc together. It is generally recommended that only {type}-pre is used, as this provides the optimal implementation in terms of speed, although the others are provided for compatibility with existing Javascript sort functions.

    Additionally, many plug-ins at http://datatables.net/plug-ins/sorting/ seem to implement all three functions, adding to the confusion.

    Finally, my use case requires both a pre method and asc and desc methods, requiring me now to implement the pre method in a separate function, calling it on the two parameters passed to the asc and desc methods and thus making my code sanitize some of the input values multiple times.

  • allanallan Posts: 61,687Questions: 1Answers: 10,100 Site admin

    The comment is wrong. i'll update it. That comes from 1.9 when that was true.

    Additionally, many plug-ins at http://datatables.net/plug-ins/sorting/ seem to implement all three functions, adding to the confusion.

    They do, for 1.9 compatibility only. If you look at the -desc and -asc parts of those plug-ins you will see that they are all trivial sorts, proving that there is no need for them.

    Finally, my use case requires both a pre method and asc and desc methods

    That's fine. Do as you say and simply have an external function that you call. A number of the other plug-ins (natural sorting for example) also do that.

    Alternatively, you are of course welcome to modify DataTables to suit your need - it is open source after all :-)

    Allan

  • allanallan Posts: 61,687Questions: 1Answers: 10,100 Site admin
    edited May 2014

    Actually - I just grepped the code and can't find the comment you mention anywhere in the source as you say. Can you point me to where it is so I can correct it?

    Allan

This discussion has been closed.