File size

Sort abbreviated file sizes correctly (8MB, 4KB, etc)

  • Author: Allan Jardine -

When dealing with computer file sizes, it is common to append a post fix such as B, KB, MB or GB to a string in order to easily denote the order of magnitude of the file size. This plug-in allows sorting to take these indicates of size into account.

A counterpart type detection plug-in is also available.

Plug-in code

jQuery.fn.dataTable.ext.type.order['file-size-pre'] = function ( data ) {
    var matches = data.match( /^(\d+(?:\.\d+)?)\s*([a-z]+)/i );
    var multipliers = {
        b:  1,
        bytes: 1,
        kb: 1000,
        kib: 1024,
        mb: 1000000,
        mib: 1048576,
        gb: 1000000000,
        gib: 1073741824,
        tb: 1000000000000,
        tib: 1099511627776,
        pb: 1000000000000000,
        pib: 1125899906842624

    if (matches) {
        var multiplier = multipliers[matches[2].toLowerCase()];
        return parseFloat( matches[1] ) * multiplier;
    } else {
        return -1;


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!


$('#example').DataTable( {
     columnDefs: [
       { type: 'file-size', targets: 0 }
  } );