Horje
sort string mixed with numbers javascript Code Example
sort string mixed with numbers javascript
const sortAlphaNum = (a, b) => a.localeCompare(b, 'en', { numeric: true })
console.log(['A1', 'A10', 'A11', 'A12', 'A2', 'A3', 'A4', 'B10', 'B2', 'F1', 'F12', 'F3'].sort(sortAlphaNum))
sort string mixed with numbers javascript
var reA = /[^a-zA-Z]/g;
var reN = /[^0-9]/g;

function sortAlphaNum(a, b) {
  var aA = a.replace(reA, "");
  var bA = b.replace(reA, "");
  if (aA === bA) {
    var aN = parseInt(a.replace(reN, ""), 10);
    var bN = parseInt(b.replace(reN, ""), 10);
    return aN === bN ? 0 : aN > bN ? 1 : -1;
  } else {
    return aA > bA ? 1 : -1;
  }
}
console.log(
["A1", "A10", "A11", "A12", "A2", "A3", "A4", "B10", "B2", "F1", "F12", "F3"].sort(sortAlphaNum)
)




Javascript

Related
javascript execute function after async Code Example javascript execute function after async Code Example
make a button who disable scrolling down the page react Code Example make a button who disable scrolling down the page react Code Example
proxy api javascript set Code Example proxy api javascript set Code Example
js what does var mean Code Example js what does var mean Code Example
how to set three js canvas width 100% Code Example how to set three js canvas width 100% Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
7