0

I'm trying to modify sorttable.js to add the option to sort alphanumerically.

Right now, if you sort by alpha, strings with numbers in them will sort like this:

  • String 1
  • String 10
  • String 100
  • String 2

If I make it sort numeric, it ignores the alpha characters when sorting. I'm trying to figure out how to combine the two functions to make it sort by both at once. Here are the two functions:

  sort_numeric: function(a,b) {
    aa = parseFloat(a[0].replace(/[^0-9.-]/g,''));
    if (isNaN(aa)) aa = 0;
    bb = parseFloat(b[0].replace(/[^0-9.-]/g,''));
    if (isNaN(bb)) bb = 0;
    return aa-bb;
  },
  sort_alpha: function(a,b) {
    if (a[0]==b[0]) return 0;
    if (a[0]<b[0]) return -1;
    return 1;
  },

Could anybody provide any pointers on how I might begin?

sth
  • 222,467
  • 53
  • 283
  • 367
Works for a Living
  • 1,262
  • 2
  • 19
  • 44

1 Answers1

1

in you function, get the 2 numbers. convert them to strings. figure out which is the longest. add "leading zeros" to the other one, then sort alpha as usual.

AwokeKnowing
  • 7,728
  • 9
  • 36
  • 47
  • This works too. Thank you very much. However, I'm implementing the script Bryan linked to, because I trust it more than my own abilities to modify scripts. :) – Works for a Living Oct 14 '13 at 18:46