Title: Custom Sorting
Last modified: August 21, 2016

---

# Custom Sorting

 *  Resolved [2xUeL](https://wordpress.org/support/users/2xuel/)
 * (@2xuel)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/custom-sorting-2/)
 * Hi Tobias,
 * Thanks for all your great “customer service” thus far 😉 I wanted to ask you 
   a couple questions about custom sorting:
 * 1. I found a support thread about making the sort so blanks go to the bottom (
   [http://wordpress.org/support/topic/ignore-empty-cells-when-sorting-asc?replies=20](http://wordpress.org/support/topic/ignore-empty-cells-when-sorting-asc?replies=20)),
   and I honestly spent quite a bit of time trying to follow the conversation and
   trying to learn more at datatables.net, but alas I really don’t know Javascript
   and I’m still confused (plus it seemed that the support thread above morphed 
   into a fairly specific case that doesn’t apply to me). Is there a more generalized
   way you could explain how to implement a function so blanks sort to the bottom
   of a column (ascending and descending)?
 * 2. I would like to have the ability to make a column with only a handful of different
   entry types sort whatever way I want. For example, if every entry in a column
   is either “bass”, “drums”, or “piano”, I would like to make it so the ascending
   sort order is drums > bass > piano–or any other order I desire for that matter.
 * 3. How does one combine the two ideas above? For example, the the entry option
   are “bass”, “drums”, “piano”, and blank, how would one make it so the sort order
   is drums > bass > piano > blank?
 * I don’t have any sort of relevant table up on my site quite yet, but if you’d
   like to see what I’m working with I can throw something up.
 * Thank you for any assistance you are able to provide 🙂
 * [https://wordpress.org/plugins/tablepress/](https://wordpress.org/plugins/tablepress/)

Viewing 1 replies (of 1 total)

 *  Plugin Author [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * (@tobiasbg)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/custom-sorting-2/#post-4593977)
 * Hi,
 * thanks for your question!
 * 1. Having blank cells go to the bottom for all sorting directions (ascending 
   and descending) is a tricky and it requires a custom sorting algorithm and implementation,
   indeed.
    In general, you’ll need to make sure that empty cells are treated as“
   bigger” than a non-empty value for ascending sort, but “smaller” than a non-empty
   value for descending sort. The code from [http://wordpress.org/support/topic/ignore-empty-cells-when-sorting-asc?replies=20#post-3747915](http://wordpress.org/support/topic/ignore-empty-cells-when-sorting-asc?replies=20#post-3747915)
   should actually do that, for plain strings.
 * 2. When sorting only a couple of strings (but not alphabetically), the “enum”
   type from [http://datatables.net/plug-ins/sorting#enum](http://datatables.net/plug-ins/sorting#enum)
   should be a good start.
 * 3. In order to merge those two, you could start with the “enum” type and add 
   some handling for empty cells (which then are treated as mentioned in my answer
   to 1).:
 *     ```
       jQuery.extend( jQuery.fn.dataTableExt.oSort, {
       	"enum-pre": function ( a ) {
       		// Add / alter the switch statement below to match your enum list
       		switch( a ) {
       			case "":      return 0;
       			case "Drums": return 1;
       			case "Bass":  return 2;
       			case "Piano": return 3;
       			default:      return 4;
       		}
       	},
   
       	"enum-asc": function ( a, b ) {
       		if ( 0 == a ) return 1;
       		if ( 0 == b ) return -1;
       		return ((a < b) ? -1 : ((a > b) ? 1 : 0));
       	},
   
       	"enum-desc": function ( a, b ) {
       		if ( 0 == a ) return -1;
       		if ( 0 == b ) return 1;
       		return ((a < b) ? 1 : ((a > b) ? -1 : 0));
       	}
       } );
       ```
   
 * This function basically turns each of the valid strings into a number. The empty
   cell is represented by a 0. Then, in the actual sorting callbacks, depending 
   on which of the two values is 0, the return value of the callback is chosen so
   that the value of with the 0 will be sorted to the bottom of the table.
 * To use this algorithm, you can add the code from above to the two JS files of
   the Sorting Plugins Extension from [http://tablepress.org/extensions/datatables-sorting-plugins/](http://tablepress.org/extensions/datatables-sorting-plugins/)
   and then add something like
 *     ```
       "aoColumnDefs": [ { "sType": "enum", "aTargets": [ 2, 3 ] } ]
       ```
   
 * to the “Custom Commands” textfield on the “Edit” screen of the table. The numbers
   2 and 3 indicate that the third and fourth column would get this enum sorting(
   counting starts with 0 in the code).
 * Regards,
    Tobias

Viewing 1 replies (of 1 total)

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

## Tags

 * [Blank](https://wordpress.org/support/topic-tag/blank/)
 * [custom](https://wordpress.org/support/topic-tag/custom/)
 * [datatables](https://wordpress.org/support/topic-tag/datatables/)
 * [sort](https://wordpress.org/support/topic-tag/sort/)

 * 1 reply
 * 2 participants
 * Last reply from: [Tobias Bäthge](https://wordpress.org/support/users/tobiasbg/)
 * Last activity: [12 years, 3 months ago](https://wordpress.org/support/topic/custom-sorting-2/#post-4593977)
 * Status: resolved