Sort mixed alpha/numeric array
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))
Sort mixed alpha/numeric Array in javascript
You can use parseInt
to get the number part and sort
it. If both a
and b
have the same number, then sort them based their length
. If they both have the same length, then sort them alphabetically using localeCompare
let array = ['1','2A','2B','2AA','2','10A','10','11','12A','12B','12'];
array.sort((a, b) => parseInt(a) - parseInt(b) || a.length - b.length || a.localeCompare(b)); console.log(array)
Javascript sort mixed alphanumeric array - letters ascending, numbers descending
If you have always a single letter, you could sort by the numerical rest and by the first letter.
const
array = ['A1', 'A4', 'A2', 'A3', 'C2', 'C4', 'C3', 'B3', 'B5', 'B4'];
array.sort((a, b) => a[0].localeCompare(b[0]) || b.slice(1) - a.slice(1));
console.log(...array);
Javascript Sort mixed alpa/numerical in the format A1, B1, C1 etc
Some like this could help
const sortAlphaNum = (a, b) => parseInt(a.replaceAll(/[A-Z]+/ig, '')) - parseInt(b.replaceAll(/[A-Z]+/ig, '')) || a.localeCompare(b);
What it is doing is removing any letter from the value and comparing just the numbers, if the numbers are the same then sort using localeCompare
Sort list of mixed alpha-numeric strings with dots?
Pulled together numerous other code snippets to find a solution. See below:
function customSort(data, key, order) { function isNumber(v) { return (+v).toString() === v; }
function isRoman(s) { // http://stackoverflow.com/a/267405/1447675 return /^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/i.test(s); }
function parseRoman(s) { var val = { M: 1000, D: 500, C: 100, L: 50, X: 10, V: 5, I: 1 }; return s.toUpperCase().split('').reduce(function (r, a, i, aa) { return val[a] < val[aa[i + 1]] ? r - val[a] : r + val[a]; }, 0); }
var sort = { asc: function (a, b) { var i = 0, l = Math.min(a.value.length, b.value.length);
while (i < l && a.value[i] === b.value[i]) { i++; } if (i === l) { return a.value.length - b.value.length; } if (isNumber(a.value[i]) && isNumber(b.value[i])) { return a.value[i] - b.value[i]; } if (isRoman(a.value[i]) && isRoman(b.value[i])) { return parseRoman(a.value[i]) - parseRoman(b.value[i]); } return a.value[i].localeCompare(b.value[i]); }, desc: function (a, b) { return sort.asc(b, a); } }; var mapped = data.map(function (el, i) { var string = el[key].replace(/\d(?=[a-z])|[a-z](?=\.)/gi, '$&. .'), regex = /(\d+)|([^0-9.]+)/g, m, parts = [];
while ((m = regex.exec(string)) !== null) { parts.push(m[0]); } return { index: i, value: parts, o: el, string: string }; });
mapped.sort(sort[order] || sort.asc); return mapped.map(function (el) { return data[el.index]; });}
var arr = [ { doc: 'Doc10', tab: '7.3' }, { doc: 'Doc2', tab: '7B' }, { doc: 'Doc13', tab: '7.20' }, { doc: 'Doc0', tab: '7' }, { doc: 'Doc1', tab: '7A' }, { doc: 'Doc3', tab: '7C' }, { doc: 'Doc4', tab: '7.0001' }, { doc: 'Doc5', tab: '7.01' }, { doc: 'Doc6', tab: '7.01A' }, { doc: 'Doc7', tab: '7.1' }, { doc: 'Doc8', tab: '7.1A' }, { doc: 'Doc9', tab: '7.2' }, { doc: 'Doc11', tab: '7.10' }, { doc: 'Doc12', tab: '7.11' }, { doc: 'Doc14', tab: '7.34' }];
const myArray = customSort(arr, 'tab');
let html = '';
for (let i = 0; i < myArray.length; i++) { html += '<li>' + myArray[i].doc + '</li>';}
document.getElementById('results').innerHTML = html;
<ul id="results" />
Sort for Array of Objects with AlphaNumeric
You could split the value and take a numerical value for the given size.
var data = [{ size: "40 SHORT", avail: true }, { size: "46 LONG", avail: true }, { size: "42 SHORT", avail: true }, { size: "40 REG", avail: true }, { size: "42 LONG", avail: true }, { size: "42 REG", avail: true }, { size: "44 REG", avail: true }, { size: "44 LONG", avail: true }, { size: "46 REG", avail: true }, { size: "48 REG", avail: true }, { size: "44 SHORT", avail: true }, { size: "38 REG", avail: true }, { size: "40 LONG", avail: true }, { size: "48 LONG", avail: true }, { size: "38 SHORT", avail: true }];
data.sort(function (a, b) { function getParts(o) { var array = o.size.split(' '); return { value: array[0], size: { SHORT: 1, REG: 2, LONG: 3 }[array[1]] }; } var aa = getParts(a), bb = getParts(b); return aa.size - bb.size || aa.value - bb.value;});
console.log(data)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Related Topics
Set Additional Data to Highcharts Series
How to Capitalize First Letter of Each Word, Like a 2-Word City
Template String as Object Property Name
How to Limit Google Autocomplete Results to City and Country Only
What Is Ajax and How Does It Work
Differencebetween JavaScript Promises and Async Await
Check If Every Element in One Array Is in a Second Array
Adding Custom Properties to a Function
Force Download Through Js or Query
How to Programatically Select an HTML Option Using JavaScript
Sort Mixed Alpha/Numeric Array
Is Node.Js Native Promise.All Processing in Parallel or Sequentially
iPad Web App: Detect Virtual Keyboard Using JavaScript in Safari
Tolocaledatestring() Changes in Ie11
React-Router: No Not Found Route