---
title: "DataTable Custom Sorting On Column"
author: "caiwenhn2008"
date: "2016-06-07T18:28:55Z"
tags: []
categories: ["企业架构"]
slug: "datatable-custom-sorting-on-column"
draft: false
lastmod: "2016-06-07T18:36:04Z"
id: 2303872
url: "https://caerun.iteye.com/blog/2303872"
---

```java
Requirement: we want to filter out the rows which is with empty value("", or " ") when sorting, always leave the rows with empty value column to the bottom of the data table. 
```



```java
jQuery.fn.dataTableExt.oSort['customstring-asc'] = function(x,y) {
		var retVal;
		x = $.trim(x);
		y = $.trim(y);

		if (x==y) retVal= 0;
		else if (x == "" || x == " ") retVal= 1;
		else if (y == "" || y == " ") retVal= -1;
		else if (x > y) retVal= 1;
		else retVal = -1;
		return retVal;
	};
	
	jQuery.fn.dataTableExt.oSort['customstring-desc'] = function(x,y) {
		var retVal;
		x = $.trim(x);
		y = $.trim(y);

		if (x==y) retVal= 0;
		else if (x == "" || x == " ") retVal= 1;
		else if (y == "" || y == " ") retVal= -1;
		else if (x > y) retVal= -1;
		else retVal = 1;
		return retVal;
	};
	
	/** Initiate Data Table * */
	oTable = $('#activityTable').dataTable({
		"oLanguage" : {
			"sSearch" : "SEARCH FLOW:"
		},
		bJQueryUI : true,
		bLengthChange : false,
		bPaginate : false,
		bInfo : false,
		aoColumnDefs : [  {
			"stype" : "customstring",
			"atargets" : [ 1 ]
		}
	});

```



Refer to <https://datatables.net/examples/plug-ins/sorting_sType.html>