Datatables not sorting properly on values returned by "render" function

Datatables not sorting properly on values returned by "render" function

rotorboyrotorboy Posts: 18Questions: 4Answers: 0

I have the following list of strings in a column, which when sorted properly by way of "render" function numeric extractor regexp, would look like this:

104
105
115
314
320
1141a
3180a

However, proper sorting is not occurring, and I get the following:

104
105
1141a
115
314
3180a
320

The "render" function is returning correct numeric values to sort by based on following:

var ext = '';
if ( type === "sort" || type === 'type' ) {
   value = ((ext = /^\s*(\d+)(?:[^\d]+[0-9]*)*$/.exec(data)) ? parseInt(ext['1']) : data); 
   return value;
}

but Datatables is still sorting by the actual column string value, NOT the value returned by render function.

While there is default ordering enabled, this ordering results from the sort buttons clicked at top of this column, so the render function should be providing the values for the sort.

Any ideas welcome.

Thanks

Replies

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769

    I built a test case for you. Looks like it works:
    http://live.datatables.net/rixusoja/1/edit

    Please update the test case to show the issue.

    Kevin

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769

    Also there is the natural sorting plugin which may work for your case.

    Kevin

  • rotorboyrotorboy Posts: 18Questions: 4Answers: 0

    Hey Kevin

    Thanks for getting back to me and writing that test script. I knew my function was returning correct values, but glad to verify it, and DT functionality as well.

    That the render function and my algorithm works imply that some other columnDef setting or column sorting mechanism is missing, set, or otherwise negatively interacting with the explicit column sort in a way that is not obvious, or that I'm misunderstanding. As such, I'm not quite sure what definitions I have in my table to add to your test case to get it to demonstrate the issue.

    To be sure, the table does have other columndef targets which have "orderData" and "orderSequence", but I assume those only apply when DataTables is sorting for "display" (which itself is already using an external sort plugin). My understanding is that when an explicit sort button is pressed on a column, ONLY that coulmnDef's render function is called with type=sort, and only that would be used to set the new row order. Is that correct?

    If that is not true, do I need to set some additional options on the columnDef in question to enforce that?

    As an aside, would be nice to determine via some sort of "explain tool" to see how dataTables walks all the columnDefs and resolves the different orderData elements of each ColumnDef into a "final sort" order.

  • rotorboyrotorboy Posts: 18Questions: 4Answers: 0

    Hey Kevin;

    Actually found the issue; mixing ints and strings.

    If you look at the data I added to test case, the sort function returns an int when regex is satisfied, and the data when not.

    This mixes ints with strings, and apparently DataTables has an issue with that.

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769

    Actually found the issue; mixing ints and strings.

    Datatables uses type detection to determine the type of data in the column for sorting. The type has to be the same for all data otherwise it defaults to string. You are using || type === 'type which you can use to influence the data type for the column. I updated the test case and it looks like value is always number:
    http://live.datatables.net/rixusoja/3/edit

    Note I change some of the data from strings to int.

    If you look at the data I added to test case,

    If you update the test case the URL will change. Did you update the test case? If so please post the new URL.

    Kevin

  • rotorboyrotorboy Posts: 18Questions: 4Answers: 0
    edited July 2020

    Hey Kevin;

    Ahhh. Yes, I had updated the test case but didn't see any change to the URL, so figured it just saved in-line.

    Didn't realize that column values all must be one type or type then defaults to string for sorting. That is ... shall we say ... a bummer. Would be nice to be able to mix modes, so all numbers are sorted, then all non-numbers are sorted...

    Thanks again for your prompt and on-point help. Very much appreciated... Long live DataTables...

  • kthorngrenkthorngren Posts: 20,302Questions: 26Answers: 4,769

    Would be nice to be able to mix modes, so all numbers are sorted, then all non-numbers are sorted.

    You can create your own sorting plugin to handle the ordering the way you want.

    Kevin

This discussion has been closed.