0

I want to sort a table using table sorter in Javascript. But when there are alphanumeric data in the column, then it is not sorting properly.

For instance:

order1023
order1145
quote786
invoice1253
quote1010

I tried by giving headers: { 0: {sorter: 'text'}}, textExtraction: "complex"} But I am getting result as:

quote1010
order1023
order1145
invoice1253
quote786

I want result as

invoice1253
order1023
order1145
quote786
quote1010
1
  • "using table sorter" - Which one? Commented Aug 29, 2013 at 11:21

1 Answer 1

1

you should add a parser as bellow,

$.tablesorter.addParser({
  id: 'alphanum',
  is: function(s) {
    return false;
  },
  format: function(s) {
    var str = s.replace(/(\d{1,2})/g, function(a){
        return pad(a);
    });

    return str;
  },
  type: 'text'
});

function pad(num ){
  var s = '00000' + num;
  return s.substr(s.length-5);
}    

when initializing,

$(function() { 
    $("table").tablesorter({ 
        headers: { 
            6: { // column number
                sorter:'alphanum' 
            } 
        } 
    }); 
}); 
Sign up to request clarification or add additional context in comments.

2 Comments

everything working fine.. but we have to use "/d+" in the place of "/d{1,2}".. why becoz we dont know the size of the number..
if you find the answer here, just mark it as correct, that will help other users.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.