filtering and sorting

filtering and sorting

PhuPhu Posts: 3Questions: 0Answers: 0
edited February 2012 in General
i would like to filter out some strings (e.g. articles like "the", "a", "an" ...), when sorting a column. so that items like "new yorker" and "the new yorker" should appear in neighboring rows.
any hints how to accomplish that?

thanks, phu

Replies

  • PhuPhu Posts: 3Questions: 0Answers: 0
    [quote]
    To add a new sort function to DataTables you need to attach your function to the object $.fn.dataTableExt.oSort.
    [/quote]
    see http://datatables.net/development/sorting

    so i did something like this:
    [code]
    jQuery.fn.dataTableExt.oSort['my-sorting-asc'] = function(a,b) {

    a = a.replace(/<.*?>/g, ""); //exclude html tags
    a = a.toLowerCase();
    a = a.replace(/ä/g, "ae"); //replace german Umlauts
    a = a.replace(/ö/g, "ue");
    a = a.replace(/ü/g, "ue");
    a = a.replace(/ß/g, "ss");
    a = a.replace(/^der\s|^die\s|^das\s/i, ""); //exclude some german articles

    b = b.replace(/<.*?>/g, "");
    b = b.toLowerCase();
    b = b.replace(/ä/g, "ae");
    b = b.replace(/ö/g, "ue");
    b = b.replace(/ü/g, "ue");
    b = b.replace(/ß/g, "ss");
    b = b.replace(/^der\s|^die\s|^das\s/i, "");

    return (a == b) ? 0 : (a > b) ? 1 : -1;
    };
    [/code]

    and

    [code]
    $(document).ready(function(){
    $('#example').dataTable({
    "aoColumns": [
    { "sType": "my-sorting" }, //custom sorting
    null, //default sorting
    null,
    null,
    null
    ],
    }
    });
    });
    [/code]
  • bnw_bullbnw_bull Posts: 1Questions: 0Answers: 0
    Hi

    the post is very interesting and used as a reference for a new function to sorting string pt-br / iso-8859-1. See:

    [code]
    //
    // function to sorting string pt-br
    // reference: http://datatables.net/forums/discussion/8348/filtering-and-sorting/p1
    // date: fev/2012
    //
    // use: "aoColumns": [ { "sType": "rcb-sorting" } ]
    //
    jQuery.fn.dataTableExt.oSort['rcb-sorting-asc'] = function(a,b) {
    a = a.replace(/<.*?>/g, ""); //exclude html tags
    a = a.toLowerCase();
    a = a.replace(/á/, "a");
    a = a.replace(/à/, "a");
    a = a.replace(/â/, "a");
    a = a.replace(/ã/, "a");
    a = a.replace(/é/, "e");
    a = a.replace(/ê/, "e");
    a = a.replace(/í/, "i");
    a = a.replace(/ó/, "o");
    a = a.replace(/õ/, "o");
    a = a.replace(/ô/, "o");
    a = a.replace(/ú/, "u");
    a = a.replace(/ü/, "u");
    a = a.replace(/ç/, "c");
    a = a.replace(/ /, "");
    a = a.replace(/\//, "");
    a = a.replace(/-/, "");
    a = a.replace(/:/, "");

    b = b.replace(/<.*?>/g, ""); //exclude html tags
    b = b.toLowerCase();
    b = b.replace(/á/, "a");
    b = b.replace(/à/, "a");
    b = b.replace(/â/, "a");
    b = b.replace(/ã/, "a");
    b = b.replace(/é/, "e");
    b = b.replace(/ê/, "e");
    b = b.replace(/í/, "i");
    b = b.replace(/ó/, "o");
    b = b.replace(/õ/, "o");
    b = b.replace(/ô/, "o");
    b = b.replace(/ú/, "u");
    b = b.replace(/ü/, "u");
    b = b.replace(/ç/, "c");
    b = b.replace(/ /, "");
    b = b.replace(/\//, "");
    b = b.replace(/-/, "");
    b = b.replace(/:/, "");

    return (a == b) ? 0 : (a > b) ? 1 : -1;
    };

    jQuery.fn.dataTableExt.oSort['rcb-sorting-desc'] = function(a,b) {
    a = a.replace(/<.*?>/g, ""); //exclude html tags
    a = a.toLowerCase();
    a = a.replace(/á/, "a");
    a = a.replace(/à/, "a");
    a = a.replace(/â/, "a");
    a = a.replace(/ã/, "a");
    a = a.replace(/é/, "e");
    a = a.replace(/ê/, "e");
    a = a.replace(/í/, "i");
    a = a.replace(/ó/, "o");
    a = a.replace(/õ/, "o");
    a = a.replace(/ô/, "o");
    a = a.replace(/ú/, "u");
    a = a.replace(/ü/, "u");
    a = a.replace(/ç/, "c");
    a = a.replace(/ /, "");
    a = a.replace(/\//, "");
    a = a.replace(/-/, "");
    a = a.replace(/:/, "");

    b = b.replace(/<.*?>/g, ""); //exclude html tags
    b = b.toLowerCase();
    b = b.replace(/á/, "a");
    b = b.replace(/à/, "a");
    b = b.replace(/â/, "a");
    b = b.replace(/ã/, "a");
    b = b.replace(/é/, "e");
    b = b.replace(/ê/, "e");
    b = b.replace(/í/, "i");
    b = b.replace(/ó/, "o");
    b = b.replace(/õ/, "o");
    b = b.replace(/ô/, "o");
    b = b.replace(/ú/, "u");
    b = b.replace(/ü/, "u");
    b = b.replace(/ç/, "c");
    b = b.replace(/ /, "");
    b = b.replace(/\//, "");
    b = b.replace(/-/, "");
    b = b.replace(/:/, "");

    return (a == b) ? 0 : (a > b) ? -1 : 1;
    };
    [/code]
  • allanallan Posts: 61,903Questions: 1Answers: 10,148 Site admin
    Thanks for posting your solution. There is a plug-in here ( http://datatables.net/plug-ins/sorting#anti_the ) which strips the work "the" from the start of strings. That could be expanded upon, should anyone else be interested in this.

    Allan
  • PhuPhu Posts: 3Questions: 0Answers: 0
    edited April 2012
    this is what i use in the end:

    [code]

    var stringsToExclude = ["der", "die", "das", "den", "dem", "des", "ein", "eine", "einen", "einem", "eines", "the", "a", "an", "la", "le", "les", "un", "une", "des", "l'"];
    function prepareForSorting(str) {
    "use strict";
    str = str.replace(/<.*?>/g, ""); //exclude html tags
    str = str.toLowerCase();
    str = str.replace(/ä/g, "ae"); //replace german umlauts
    str = str.replace(/ö/g, "ue");
    str = str.replace(/ü/g, "ue");
    str = str.replace(/ß/g, "ss");
    for (var i = 0; i < stringsToExclude.length; i++) {
    var myRegex = "^"+stringsToExclude[i]+"\\s"; //we need to escape special characters for use in the regex object constructor, e.g. \\s instead of \s
    str = str.replace(new RegExp(myRegex, "i"), ""); }
    return str;
    }
    jQuery.fn.dataTableExt.oSort['my-sorting-asc'] = function(a,b) {
    a = prepareForSorting(a);
    b = prepareForSorting(b);
    return (a == b) ? 0 : (a > b) ? 1 : -1;
    };
    jQuery.fn.dataTableExt.oSort['my-sorting-desc'] = function(a,b) {
    a = prepareForSorting(a);
    b = prepareForSorting(b);
    return (a == b) ? 0 : (a > b) ? -1 : 1; //reverse sorting
    };

    [/code]
This discussion has been closed.