Created
June 13, 2016 22:29
-
-
Save bryceosterhaus/907ae5bd98e99793af7d15e8228919ce to your computer and use it in GitHub Desktop.
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
packages/sort-lines/lib/sort-lines.coffee | |
sortLinesNatural = (editor) -> | |
sortTextLines editor, (textLines) -> | |
naturalSortRegex = /^(\d*)(\D*)(\d*)([\s\S]*)$/ | |
textLines.sort (a, b) => | |
a = a.toLowerCase() | |
b = b.toLowerCase() | |
return 0 if a is b | |
[__, aLeadingNum, aWord, aTrailingNum, aRemainder] = naturalSortRegex.exec(a) | |
[__, bLeadingNum, bWord, bTrailingNum, bRemainder] = naturalSortRegex.exec(b) | |
return (if a < b then -1 else 1) if aWord isnt bWord | |
return (if aLeadingNum < bLeadingNum then -1 else 1) if aLeadingNum isnt bLeadingNum | |
return (if aTrailingNum < bTrailingNum then -1 else 1) if aTrailingNum isnt bTrailingNum | |
return 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment