MomentJs Sorting

by Akram kamal

HTML

<script src="https://cdn.datatables.net/1.7.5/js/jquery.dataTables.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
 <table id="table">
    <thead>
       <tr>
         <th>Date</th>
       </tr>
    </thead>
    <tbody>
      <tr>
         <td>March 2015</td>
      </tr>
      <tr>
         <td>January 2015</td>
      </tr>
      <tr>
         <td>December 2014</td>
      </tr>
      <tr>
         <td>August 2015</td>
      </tr>
    </tbody>
</table>

JavaScript

/**
 * This plug-in for DataTables represents the ultimate option in extensibility
 * for sorting date / time strings correctly. It uses
 * [Moment.js](http://momentjs.com) to create automatic type detection and
 * sorting plug-ins for DataTables based on a given format. This way, DataTables
 * will automatically detect your temporal information and sort it correctly.
 *
 * For usage instructions, please see the DataTables blog
 * post that [introduces it](//datatables.net/blog/2014-12-18).
 *
 * @name Ultimate Date / Time sorting
 * @summary Sort date and time in any format using Moment.js
 * @author [Allan Jardine](//datatables.net)
 * @depends DataTables 1.10+, Moment.js 1.7+
 *
 * @example
 *    $.fn.dataTable.moment( 'HH:mm MMM D, YY' );
 *    $.fn.dataTable.moment( 'dddd, MMMM Do, YYYY' );
 *
 *    $('#example').DataTable();
 */

(function($) {

	$.fn.dataTable.moment = function ( format, locale ) {
		var types = $.fn.dataTableExt.aTypes;

		// Add type detection
		types.unshift( function ( d ) {
			// Null and empty values are acceptable
			if ( d === '' || d === null ) {
				return 'moment-'+format;
			}

			return moment( d.replace ? d.replace(/<.*?>/g, '') : d, format, locale, true ).isValid() ?
			'moment-'+format :
				null;
		} );

		function parseFormatToUnix(value, format, locale) {
			return value === '' || value === null ?
				-Infinity :
				parseInt( moment( value.replace ? value.replace(/<.*?>/g, '') : value, format, locale, true ).format( 'x' ), 10 );
		}

		// Add ascending sorting method
		$.fn.dataTableExt.oSort[ 'moment-'+format+'-asc' ] = function ( x, y ) {

			var parsedX = parseFormatToUnix(x, format, locale);
			var parsedY = parseFormatToUnix(y, format, locale);

			return parsedX - parsedY;
		};

		// Add descending sorting method
		$.fn.dataTableExt.oSort[ 'moment-'+format+'-desc' ] = function ( x, y ) {
			var parsedX = parseFormatToUnix(x, format, locale);
			var parsedY = parseFormatToUnix(y, format, locale);

			return parsedY...