how to sort mixed numeric/alphanumeric array in javascript how to sort mixed numeric/alphanumeric array in javascript angularjs angularjs

how to sort mixed numeric/alphanumeric array in javascript


var arr = ['A1', 'A10', 'A11', 'A12', 'A3A', 'A3B', 'A3', 'A4', 'B10', 'B2', 'F1', '1', '2', 'F3'];// regular expression to get the alphabetic and the number parts, if anyvar regex = /^([a-z]*)(\d*)/i;function sortFn(a, b) {  var _a = a.match(regex);  var _b = b.match(regex);  // if the alphabetic part of a is less than that of b => -1  if (_a[1] < _b[1]) return -1;  // if the alphabetic part of a is greater than that of b => 1  if (_a[1] > _b[1]) return 1;  // if the alphabetic parts are equal, check the number parts  var _n = parseInt(_a[2]) - parseInt(_b[2]);  if(_n == 0) // if the number parts are equal start a recursive test on the rest      return sortFn(a.substr(_a[0].length), b.substr(_b[0].length));  // else, just sort using the numbers parts  return _n;}console.log(arr.sort(sortFn));