Sorting data by number while ignoring leading alpha

Sorting data by number while ignoring leading alpha

dtarvindtarvin Posts: 7Questions: 2Answers: 0
edited April 2018 in Free community support

Hi, I'm working on some code that is supposed to list document numbers in descending order. However, some documents have a letter at the beginning of the number and some don't. The result is that the document numbers are split between the ones with the letter, sorted in descending order, and the ones without the letter coming after, sorted in descending order. For example:

a85065
a85053
a85041
85063
85052

What I want to do is sort the documents by numerical order while ignoring the letter. For example:

a85065
85063
a85053
85052
a85041

The problem is I still need the letter to display on the document number, so it is not as easy as popping off the letter. Does anyone have any ideas?

Here is the code for the sort as-is:

j$("table[id$=documentsReceivedBlock]").DataTable({
    "order": [[0, 'desc']],
    "bFilter": false,
    "bPaginate": false,
    "bInfo": false,
    "columnDefs: [
        { "type": "date-sort", "targets": [1,2,5] }
    ]
});

Thanks!

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi @dtarvin ,

    The columns.render can return different representations of the same data depending upon it's purpose, i.e. displaying, sorting, filtering, etc. If you use that, as in this example here, it'll do what you want,

    Cheers,

    Colin

  • dtarvindtarvin Posts: 7Questions: 2Answers: 0

    Thanks, but I just can't seem to get it to work.

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    My code is pretty much exactly what you want. If it's not working on your table, why don't you repost what you currently have so we can take a look.

  • dtarvindtarvin Posts: 7Questions: 2Answers: 0
    edited April 2018
    j$("table[id$=documentsReceivedBlock]").DataTable({
                "drawCallback": function (settings) {
                    j$(".popover-info").popover({
                        html: true,
                        trigger: 'hover',
                        container: 'body',
                        title: function () {
                            return j$(this).siblings().children('.popover-title').html();
                        },
                        content: function () {
                            return j$(this).siblings().children('.popover-content').html();
                        }
                    });
                },
                "order": [[0, 'desc']],
                "bFilter": false,
                "bPaginate": false,
                "bInfo": false,
                "columnDefs": [ {
                    "targets": 0,
                    "data": "documentNumber",
                    "render": function( data, type, row, meta ) {
                        return (type === 'sort')? data.replace(/\D/g,'') : data;
                    }
                }]
    
            });
    
  • dtarvindtarvin Posts: 7Questions: 2Answers: 0

    Oh, and I also tried it with the "order" commented out.

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    I'm assuming that column 0 is the column with these document numbers in? You don't need data on line 21, I'd remove that - it's only needed if you're using object based data, and then the columns.data should be declared in the columns object.

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951

    I've had good luck with the Natural Sorting plugin for this type of data.

    Kevin

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    Hi David,

    I see you posted this on StackOverflow yesterday. Is this still an issue?

    Cheers,

    Colin

  • dtarvindtarvin Posts: 7Questions: 2Answers: 0

    Sorry for the delayed response. I ended up using a regex. Thanks!

This discussion has been closed.