Title: Natural sort function missing?
Last modified: December 24, 2018

---

# Natural sort function missing?

 *  Resolved [qualitas](https://wordpress.org/support/users/qualitas/)
 * (@qualitas)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/natural-sort-function-missing/)
 * Even numbers are not sort naturaly.
 * I think the natural sort function is missing.
 * Natural sort:
    PHP “natsort” or javascript function “naturalSort” is missing?
   Info: [https://www.npmjs.com/package/javascript-natural-sort](https://www.npmjs.com/package/javascript-natural-sort)
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fnatural-sort-function-missing%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 10 replies - 1 through 10 (of 10 total)

 *  Plugin Author [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * (@tobiasbg)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/natural-sort-function-missing/#post-11024114)
 * Hi,
 * thanks for your post, and sorry for the trouble.
 * There is indeed no direct natural sorting algorithm in the DataTables JS library.
   However, normally the columns in your table can be made to sort correctly – if
   you properly move that second row with the descriptions to the table’s header
   row (which now has the 1, 2, 3, 4, 5). Otherwise, these texts will be included
   in the sorting and they will be moved with the other rows.
 * In addition, to fix a small visual bug, could you please try adding this to the“
   Custom CSS” textarea on the “Plugin Options” screen of TablePress:
 *     ```
       .tablepress {
         border-collapse: separate;
       }
       ```
   
 * Regards,
    Tobias
 *  Thread Starter [qualitas](https://wordpress.org/support/users/qualitas/)
 * (@qualitas)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/natural-sort-function-missing/#post-11026473)
 * Hi Tobias,
 * The table is correct when the page is loaded.
    The problem occurred when trying
   to sort from most column. Order turn to 1, 10, 100, 2 instead of 1, 2, 10, 100.
 * I used JSGrid before. You should try it.
 * Or there is a natural sort plug-in for datatables here:
    [https://datatables.net/plug-ins/sorting/natural](https://datatables.net/plug-ins/sorting/natural)
 * Regards
 * Yves
 *  Thread Starter [qualitas](https://wordpress.org/support/users/qualitas/)
 * (@qualitas)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/natural-sort-function-missing/#post-11026494)
 * Hi Tobias,
 * Sorry, the problem is not natural sorting.
    It doesn’t recognize the international
   metric writing of “1 000” instead of “1000” in my last column.
 * The regex needs to remove the space between numbers.
 * Regards
 * Yves
 *  Plugin Author [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * (@tobiasbg)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/natural-sort-function-missing/#post-11026631)
 * Hi,
 * to now use a proper sorting algorithm, please add this to the “Custom Commands”
   text field on the “Edit” screen of the table:
 *     ```
       "columnDefs": [ { "type": "num-fmt", "targets": [ "_all" ] } ], "language": jQuery.extend( {}, DataTables_language["fr_FR"], { "thousands": " " } )
       ```
   
 * Regards,
    Tobias
 *  Thread Starter [qualitas](https://wordpress.org/support/users/qualitas/)
 * (@qualitas)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/natural-sort-function-missing/#post-11026757)
 * Hi Tobias,
 * Thanks, work better now but only ascendant.
    Descendant still not sorting thousand
   properly.
 * Regards
 * Yves
 *  Plugin Author [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * (@tobiasbg)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/natural-sort-function-missing/#post-11027340)
 * Hi Yves,
 * it could be possible that the external DataTables JS library wants a different
   character for the thousand delimiter. Could you maybe try a `.`?
 * Regards,
    Tobias
 *  Thread Starter [qualitas](https://wordpress.org/support/users/qualitas/)
 * (@qualitas)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/natural-sort-function-missing/#post-11034752)
 * Hi Tobias,
 * Doesn’t work
 * Dot would not be international metric system.
 * Regards
 * Yves
 *  Thread Starter [qualitas](https://wordpress.org/support/users/qualitas/)
 * (@qualitas)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/natural-sort-function-missing/#post-11034778)
 * Hi Tobias,
 * The question was asked before here:
    [https://datatables.net/forums/discussion/10113](https://datatables.net/forums/discussion/10113)
 * The plug-in is here:
    [https://datatables.net/plug-ins/sorting/formatted-numbers](https://datatables.net/plug-ins/sorting/formatted-numbers)
 * But deprecated because suppose to be in Datatable.
 * But the regex removed a “-”
 * What I need is to remove a space not a – so the code should be:
    ” ”
 * Like that:
 *     ```
       jQuery.extend( jQuery.fn.dataTableExt.oSort, {
           "formatted-num-pre": function ( a ) {
               a = (a === " " || a === "") ? 0 : a.replace( /[^\d\-\.]/g, "" );
               return parseFloat( a );
           },
   
           "formatted-num-asc": function ( a, b ) {
               return a - b;
           },
   
           "formatted-num-desc": function ( a, b ) {
               return b - a;
           }
       } );
       ```
   
 * Regards
 * Yves
    -  This reply was modified 7 years, 5 months ago by [qualitas](https://wordpress.org/support/users/qualitas/).
    -  This reply was modified 7 years, 5 months ago by [qualitas](https://wordpress.org/support/users/qualitas/).
 *  Thread Starter [qualitas](https://wordpress.org/support/users/qualitas/)
 * (@qualitas)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/natural-sort-function-missing/#post-11034791)
 * Hi Tobias,
 * I found a solution here:
    [https://datatables.net/forums/discussion/20853](https://datatables.net/forums/discussion/20853)
 * by just adding HTML code `&thinsp;` instead of space is working.
 * Regards
 * Yves
    -  This reply was modified 7 years, 5 months ago by [qualitas](https://wordpress.org/support/users/qualitas/).
 *  Plugin Author [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * (@tobiasbg)
 * [7 years, 5 months ago](https://wordpress.org/support/topic/natural-sort-function-missing/#post-11036142)
 * Hi,
 * you could maybe check out the TablePress Extension from [https://tablepress.org/extensions/datatables-sorting-plugins/](https://tablepress.org/extensions/datatables-sorting-plugins/)
 * This adds some extra sorting algorithms via the included JavaScript files (non-
   minified and minified!). This also has a `formatted-num` algorithm. You could
   either try that or modify it to your needs.
 * Regards,
    Tobias

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Natural sort function missing?’ is closed to new replies.

 * ![](https://ps.w.org/tablepress/assets/icon.svg?rev=3192944)
 * [TablePress - Tables in WordPress made easy](https://wordpress.org/plugins/tablepress/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/tablepress/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/tablepress/)
 * [Active Topics](https://wordpress.org/support/plugin/tablepress/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/tablepress/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/tablepress/reviews/)

 * 10 replies
 * 2 participants
 * Last reply from: [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * Last activity: [7 years, 5 months ago](https://wordpress.org/support/topic/natural-sort-function-missing/#post-11036142)
 * Status: resolved