Sorting taking twith custom ordering takes too long

Sorting taking twith custom ordering takes too long

biswa1shawbiswa1shaw Posts: 4Questions: 2Answers: 0

I needed a custom ordering function to sort the datatable as shown in the code below. But although the sort is pretty straight forward, it takes a long time(~10 sec) for just 350 records. In the forum I read about setting orderClasses to false, but so improvements are observed. Please note that datatable works pretty well with the built in sort function, but a simple hook overwriting the default behavior brings the performance down manyfolds.
Am I missing a configuration or something.
Any help is greatly appreciated.

$(document).ready(function(){

  $.fn.dataTableExt.oSort["test_name-desc"] = function (x, y)
  {
    x = jQuery(x).text();
    y = jQuery(y).text();

    temp = x.split("-");
    xPart1 = temp[0];
    xPart2 = isNaN(temp[1]) ? 0 : parseInt(temp[1]);

    temp = y.split("-");
    yPart1 = temp[0];
    yPart2 = isNaN(temp[1]) ? 0 : parseInt(temp[1]);

    if(xPart1 > yPart1)
    {
      return -1;
    }

    if(xPart2 > yPart2)
    {
      return -1;
    }

    return 1;
  };

  $.fn.dataTableExt.oSort["test_name-asc"] = function (x, y)
  {
    x = jQuery(x).text();
    y = jQuery(y).text();

    temp = x.split("-");
    xPart1 = temp[0];
    xPart2 = isNaN(temp[1]) ? 0 : parseInt(temp[1]);

    temp = y.split("-");
    yPart1 = temp[0];
    yPart2 = isNaN(temp[1]) ? 0 : parseInt(temp[1]);

    if(xPart1 > yPart1)
    {
      return 1;
    }

    if(xPart2 > yPart2)
    {
      return 1;
    }

    return -1;
  }

  $("#stripedTableAcp").DataTable({
      "columnDefs": [
        { 
          "type": "test_name",
          "targets": 3
        } 
      ],
      "order": [[ 3, "asc" ]],
      "pageLength": 100,
      "lengthMenu": [100, 500, 1000, 2000, 5000],
      "orderClasses": false
  });
});

Answers

  • kthorngrenkthorngren Posts: 5,130Questions: 19Answers: 1,136
    edited September 2018

    Its hard to say without seeing a test case with a sample of you data. Looks like you are parsing alphanumeric data with a - between the text and numbers. Not sure if it works with dashes but I wonder if the Natural sorting plugin would work.

    I would start by putting console.log statements in the sort function to see how many times it loops through the function. Something like this:

    $.fn.dataTableExt.oSort["test_name-asc"] = function (x, y)
      {
         console.log(x, y);
    ....
    

    Otherwise we will probably need to see your page or a test case replicating the issue.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • biswa1shawbiswa1shaw Posts: 4Questions: 2Answers: 0

    Hi Kevin,
    Setting up some context into the problem. You were correct to determine that the column of interest contains a string and numberic parts separated by a - . While sorting the string takes precedence over the numbers.
    I tries the natural sorting plugin but unfortunately it does not support the - as you had anticipated.

    Here are some of the statistics gathered from the code

    I put in a console.log(1) and a console.log(2) as the first line in the descending and ascending sort functions respectively.
    For 687 rows,
    1. on page load the console prints 2 "8097" times.
    2. on clicking the table header to sort in descending order , the console shows 1 "7441" times
    3. On trying to sort in ascending order again, the console prints 2 "6778" times.
    4. Thereafter it keeps rotating between 7441 and 6778

    whats weird is the difference in the number of 1's printed during page load and manual invocation of ascenting sort.
    Also since the number of rows is constant, shouldn't the 1 and 2 be printed the same number of times. Its comparing the same number of elements.

  • colincolin Posts: 4,013Questions: 0Answers: 710

    As Kevin said, a test case with sample data would greatly help. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

Sign In or Register to comment.