0

enter image description here

The select option is not sorted in ascending order, How to sort it as number not as string please see the attached image

initComplete: function() {
        this.api().columns([0, 1, 2, 3, 4]).every(function() {
            var column = this;
            var select = $('<select onclick="event.stopPropagation()" class="form-select form-select-sm"><option value="">All</option></select>')
                .appendTo($(column.header()))
                .on('change', function() {
                    var val = $.fn.dataTable.util.escapeRegex(
                        $(this).val()
                    );

                    column
                        .search(val ? '^' + val + '$' : '', true, false)
                        .draw();
                });

            column.data().unique().sort().each(function(d, j) {
                if (d != null) {
                    select.append('<option value="' + d + '">' + d + '</option>')
                }
            });
        });
    },
andrewJames
  • 19,570
  • 8
  • 19
  • 51
  • @andrewJames Sir, how to use it in datatable? – Mr Anonymous Aug 30 '22 at 17:16
  • @andrewJames Sir , like i was sending data from java as string so i converted it to long but it didn't work, then I tried it to set it as numeric value but it doesn't work – Mr Anonymous Aug 30 '22 at 17:19
  • Sorry, my apologies. The question I linked to was for Java - I should have linked to a JavaScript question: [Javascript : natural sort of alphanumerical strings](https://stackoverflow.com/q/2802341/12567365). (Also, small point, but no need to use "sir" here!) – andrewJames Aug 30 '22 at 17:42
  • For example, you can replace your `.sort()` with `.sort((a, b) => a.localeCompare(b, undefined, { numeric: true }))`. If you have some alphanumeric columns in `.columns([0, 1, 2, 3, 4])`, then you can refine this, so only the relevant column(s) use this numeric sort, if you need that (it may depend on the specific data in each alphanumeric column). – andrewJames Aug 30 '22 at 21:52

0 Answers0