Title: Sorting by dateformat
Last modified: August 20, 2016

---

# Sorting by dateformat

 *  Resolved [Gunde](https://wordpress.org/support/users/gunde/)
 * (@gunde)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/sorting-by-dateformat/)
 * Hi
    I’ve got columns of date format, I’ve been trying to sort with no luck, yet.
 * The date format looks like this: 03 Jan 2013
    27 Dec 2012 02 Jan 2103
 * I’ve tried the sorting extension with out any results.
 * Is it not possible to sort by that date format?
 * /Gunde
 * [http://wordpress.org/extend/plugins/tablepress/](http://wordpress.org/extend/plugins/tablepress/)

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

 *  Plugin Author [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * (@tobiasbg)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/sorting-by-dateformat/#post-3330963)
 * Hi,
 * thanks for your question.
 * That date format is unfortunately not built-in into the DataTables JavaScript
   library (from [http://www.datatables.net](http://www.datatables.net)), that TablePress
   uses for the sorting.
 * It could in theory be added by custom coding, but unfortunately I don’t really
   know how that would have to look like 🙁 Sorry.
 * Can you maybe switch to another date format like
 *     ```
       dd.mm.yyyy
       ```
   
 * or
 *     ```
       dd/mm/yyyy
       ```
   
 * ?
 * Regards,
    Tobias
 *  Thread Starter [Gunde](https://wordpress.org/support/users/gunde/)
 * (@gunde)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/sorting-by-dateformat/#post-3330964)
 * Hi
 * Unfortunately I can’t (or I just can’t figure it out) switch the format because
   the date are all scraped from WP blogs “lastbuilddate”
 * I’ll sleep on it..again
 * Thx for the fast response
 * /Gunde
 *  Plugin Author [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * (@tobiasbg)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/sorting-by-dateformat/#post-3330968)
 * Hi,
 * ah, ok. I see…
    You might try to contact the developer of the DataTables library
   at [http://www.datatables.net](http://www.datatables.net). Maybe he can give 
   you the necessary JavaScript code, or at least offer to create it on a professional
   basis.
 * Best wishes,
    Tobias
 *  Thread Starter [Gunde](https://wordpress.org/support/users/gunde/)
 * (@gunde)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/sorting-by-dateformat/#post-3331159)
 * Hi Tobis
 * I’ve managed to change the format: dd/mmm/yyyy (stil not sortable)
 * I’ve look around datatables.net and found some code:
 *     ```
       // Override default implementation for date sorting
       jQuery.extend( jQuery.fn.dataTableExt.oSort, {
           "date-uk-pre": function ( a ) {
               var ukDatea = jQuery(a).text().split('-');
               if(ukDatea.length==2){
                   var month = ukDatea[0];
               }else{
                   month = ukDatea[1];
               }
               if(month=="jan" || month=="Jan"){month=01;}
               else if(month=="feb" || month=="Feb"){month=02;}
               else if(month=="mar" || month=="Mar"){month=03;}
               else if(month=="apr" || month=="Apr"){month=04;}
               else if(month=="may" || month=="May"){month=05;}
               else if(month=="jun" || month=="Jun"){month=06;}
               else if(month=="jul" || month=="Jul"){month=07;}
               else if(month=="aug" || month=="Aug"){month=08;}
               else if(month=="sep" || month=="Sep"){month=09;}
               else if(month=="oct" || month=="Oct"){month=10;}
               else if(month=="nov" || month=="Nov"){month=11;}
               else{month=12;}
               if(ukDatea.length==2){
                   return (ukDatea[1] + month) * 1;
               }else{
                   return (ukDatea[2] + month + ukDatea[0]) * 1;
               }
           },
   
           "date-uk-asc": function ( a, b ) {
               console.log(a+" "+b);
               return ((a < b) ? -1 : ((a > b) ? 1 : 0));
           },
   
           "date-uk-desc": function ( a, b ) {
               return ((a < b) ? 1 : ((a > b) ? -1 : 0));
           }
       });
       ```
   
 * [Link](http://www.datatables.net/forums/discussion/2467/need-help-for-sorting-date-with-ddmmyyyy-format/p1)
 * Is it possible to implement the code in the jQuery of the Tablepress sorting 
   extention?!?
 * /Gunde
 *  Plugin Author [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * (@tobiasbg)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/sorting-by-dateformat/#post-3331176)
 * Hi,
 * ah, very cool. Nice find!
 * To use that in TablePress, I suggest to simply append that code to the files 
   jquery.datatables.sorting-plugins.js and jquery.datatables.sorting-plugins.min.
   js
    After that, the sorting algorithms will be loaded. You will then just have
   to apply them to the desired column, by adding the following to the “Custom Commands”
   textfield in the “Features of the DataTables JavaScript library” section on the“
   Edit” screen of the table:
 *     ```
       "aoColumnDefs": [ { "sType": "html", "aTargets": [ "column-4" ] } ]
       ```
   
 * Change the “column-4” to the correct column number, i.e. change the 4 to the 
   number of the column that contains the dates in the new format.
 * Regards,
    Tobias
 *  Thread Starter [Gunde](https://wordpress.org/support/users/gunde/)
 * (@gunde)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/sorting-by-dateformat/#post-3331200)
 * Hi
 * Well it works like a charm, sorting dd/mm/yy , dd/mmm/yyyy and dd/mm/yyyy.
 * But turns out the WP scraper that I use creates blank spaces before and after
   the output, sow I can’t sort my list. I found another scraper that nearly does
   the job. (It don’t allow regex before output)
    Is it possible to run regex inside
   the table?
 * /Gunde
 *  Plugin Author [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * (@tobiasbg)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/sorting-by-dateformat/#post-3331201)
 * Hi,
 * so, you mean that your dates turn up like
 *     ```
       " 02 Jan 2103 "
       ```
   
 * instead of
 *     ```
       "02 Jan 2103"
       ```
   
 * (without the quotation marks “, but with the spaces as in the first case?
    And
   due to that, the sorting with the code you found is not working? Well, you could
   simply remove the spaces in the sorting algorithm, so that the sorting works.
   Just replace the line
 *     ```
       var ukDatea = jQuery(a).text().split('-');
       ```
   
 * with
 *     ```
       var ukDatea = jQuery(a).text();
       ukDatea = jQuery.trim( ukDatea );
       ukDatea = ukDatea.split('-');
       ```
   
 * Regards,
    Tobias
 *  Thread Starter [Gunde](https://wordpress.org/support/users/gunde/)
 * (@gunde)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/sorting-by-dateformat/#post-3331203)
 * Hi
 * In the soucecode it’s like this:
    `" 02 Jan 2103 "`
 * On the page it’s like this:
    `"02 Jan 2103"`
 * Really weird.
 * The scraper plug-in seems not to be updated any more so I’ll try the other one
   I found that’s up-to-date (but with out any functions).
 * The .trim didn’t work, but thanks anyway.
 * /Gunde
 *  Plugin Author [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * (@tobiasbg)
 * [13 years, 5 months ago](https://wordpress.org/support/topic/sorting-by-dateformat/#post-3331204)
 * Hi,
 * ah, that’s too bad. Sorry about that 🙁
    I hope that you can find a solution 
   to your problem…
 * Best wishes,
    Tobias

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

The topic ‘Sorting by dateformat’ 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/)

 * 9 replies
 * 2 participants
 * Last reply from: [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * Last activity: [13 years, 5 months ago](https://wordpress.org/support/topic/sorting-by-dateformat/#post-3331204)
 * Status: resolved