Sorting by Number of Different Lengths

Sorting by Number of Different Lengths

ParmeeParmee Posts: 3Questions: 1Answers: 0

Hey,

I'm facing a bug when sorting by a number of varying lengths. For example:

1
22
333
1111
22222
333333

When sorting the above it produces results like this:

1
1111
22
22222
333
333333

Rather than how it should be above.

Thanks,

Answers

  • ParmeeParmee Posts: 3Questions: 1Answers: 0

    It seems to work fine when using a plugin and specifying the table to use that type: https://datatables.net/plug-ins/sorting/natural

    Though, unsure why this requires a plugin and isn't automatically detected like a standard number.

    Thanks,

  • kthorngrenkthorngren Posts: 20,322Questions: 26Answers: 4,774

    I was just going to suggest that plugin. Does that column have something other than numbers in it? The column type will be set to string if there are non-numeric elements in the column. The natural sorting plugin will work regardless of column type (number or string).

    Kevin

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin

    Yes, there must be some data in the column that is non-numeric, which is causing DataTables to sort it as a string rather than as numbers.

    The natural sorting plug-in is a good option for mixed numeric and non-numeric content.

    Allan

  • ParmeeParmee Posts: 3Questions: 1Answers: 0
    edited February 2017

    Hey,

    It might be that I'm using a render to apply HTML, though I'd have assumed the data-order=" " would be used rather than the HTML field?

    Debug can be found here: http://debug.datatables.net/omuziz

    Live Test: http://live.datatables.net/pihoyugu/1/

    Screenshot: https://www.screencast.com/t/pV67QIDj

    Thanks,

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin

    Thank you for the test case. There you have:

    { type: 'natural-nohtml', targets: 0 },

    But there is no sorting plug-in of that name defined. Removing that let's it work as expected.

    My guess is that that plug-in is doing a string sort.

    Allan

This discussion has been closed.