Last active
March 31, 2018 03:23
-
-
Save westc/1d0631ce448bda990e4ad0ab4f7ab9c1 to your computer and use it in GitHub Desktop.
Compare 2 file names by grouping digits and non-digits.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function compareFileNames(a, b) { | |
if (a && b) { | |
var aParts = (a + '').match(/\d+|\D+/g) || [], | |
bParts = (b + '').match(/\d+|\D+/g) || []; | |
if (/\d/.test(aParts[0]) == /\d/.test(bParts[0])) { | |
for (var x, i = 0, l = Math.min(aParts.length, bParts.length); i < l; i++) { | |
if (aParts[i] != bParts[i]) { | |
return (/\d/.test(aParts[i]) && (x = parseInt(aParts[i], 10) - parseInt(bParts[i], 10))) | |
? x | |
: aParts[i] < bParts[i] ? -1 : 1; | |
} | |
} | |
} | |
return a < b ? a == b ? 0 : -1 : 1; | |
} | |
return a ? 1 : -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment