Dynamically create enum sorting options for a DataTable

Sort data by a defined enumerated (enum) list. The options for the values in the enum are defined by passing the values in an array to the method $.fn.dataTable.enum. Type detection and sorting plug-ins for DataTables will automatically be generated and added to the table.

For full details and instructions please see this DataTables blog post.

Plug-in code

(function ($) {

var unique = 0;
var types = $.fn.dataTable.ext.type;

// Using form $.fn.dataTable.enum breaks at least YuiCompressor since enum is
// a reserved word in JavaScript
$.fn.dataTable['enum'] = function ( arr ) {
    var name = 'enum-'+(unique++);
    var lookup = window.Map ? new Map() : {};

    for ( var i=0, ien=arr.length ; i<ien ; i++ ) {
        lookup[ arr[i] ] = i;

    // Add type detection
    types.detect.unshift( function ( d ) {
        return lookup[ d ] !== undefined ?
            name :
    } );

    // Add sorting method
    types.order[ name+'-pre' ] = function ( d ) {
        return lookup[ d ];



This plug-in is available on the DataTables CDN:


Note that if you are using multiple plug-ins, it is beneficial in terms of performance to combine the plug-ins into a single file and host it on your own server, rather than making multiple requests to the DataTables CDN.

Version control

If you have any ideas for how this plug-in can be improved, or spot anything that is in error, it is available on GitHub and pull requests are very welcome!


$.fn.dataTable.enum( [ 'High', 'Medium', 'Low' ] );