0

I am using the jQuery plugin Datables: http://www.datatables.net/

My example is here: http://live.datatables.net/wanebotu/1/edit

I have a few columns in my example table and one of them is "Name". I have added a few names that are the same but one starting in uppercase and the other all lowercase. For example: Ashton Cox, ashton cox. However, I do believe that the default sorting used in datatables is case-insensitive meaning the sorting is 'stable' (correct me if I am wrong).

My problem is that when I sort it, the column becomes inconsistent. If it's ascending, uppercase letters will come first in the sense that the first row is Ashton Cox then ashton cox. If it is descending, the last row is ashton cox, then the second last row is Ashton Cox.

Therefore, how can I write a custom sort such that the last row is uppercase first for descending, if it is the same word but capitalised.

E.g. for ascending:
Ashton Cox
ashton cox,
Colleen Hurst
colleen Hurst

E.g. for descending: 
colleen hurst,
Colleen Hurst,
ashton cox
Ashton Cox

Sorry if this all sounds convoluted. It's a confusing problem with a confusing requirement.

1

1 Answer 1

0

Got this one working with the following code:

$.extend( $.fn.dataTableExt.oSort, {
    "casetext-pre": function ( a ) {
      return a[0].toLowerCase()+a;
    },
    "casetext-asc": function ( a, b ) {
        return ((a < b) ? -1 : ((a > b) ? 1 : 0));
    },
    "casetext-desc": function ( a, b ) {
        return ((a < b) ? 1 : ((a > b) ? -1 : 0));
    }
} );

$(document).ready( function () {
  var table = $('#example').DataTable({
        "aoColumns": [{
      "sType": "casetext"
    },null,null,null,null,null]
  });
} );

This will define a new sort function/type which will convert the first character of the data to lowercase and then append the original string data.

Note that to have to setup the data type in aoColums. (In this example I only care for the name having a specific type). The amount of nulls should match your columns.

This was not tested very thoroughly, but it should give you a hint on how to proceed.

Here is your modified example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.