Created
July 9, 2017 21:26
-
-
Save dysbulic/316102113cc5ff8074b92b106c230c57 to your computer and use it in GitHub Desktop.
Unit Sorting in Javascript
This file contains hidden or 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
#!/usr/bin/env node | |
var fs = require('fs') | |
var matches = [] | |
process.argv.slice(2).forEach((filename) => { | |
try { | |
var content = fs.readFileSync(filename) | |
var lines = content.toString().split(/\r\n|\n/) | |
lines.forEach((line) => { | |
var match = line.match(/^#?(\d+)([^ ]*) - /) | |
if(match) { | |
matches.push(match) | |
} else if(line.replace(/\s+/, '') !== '') { | |
console.warn('Error:', "Unrecognized line format: " + line) | |
} | |
}) | |
} catch(error) { | |
if(error.code === 'ENOENT' || error.code === 'EACCES') { | |
console.warn('Error:', "Can't open: " + filename) | |
} else { | |
console.warn('Error:', error) | |
} | |
} | |
}) | |
matches.sort((a, b) => { | |
var comparison = parseFloat(a[1]) - parseFloat(b[1]) | |
if(comparison === 0) { | |
comparison = a[2].toLowerCase().localeCompare(b[2].toLowerCase()) | |
} | |
return comparison | |
}) | |
matches.forEach((match) => { console.log(match.input) }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment