Skip to content

Instantly share code, notes, and snippets.

@bryceosterhaus
Created June 13, 2016 22:29
Show Gist options
  • Save bryceosterhaus/907ae5bd98e99793af7d15e8228919ce to your computer and use it in GitHub Desktop.
Save bryceosterhaus/907ae5bd98e99793af7d15e8228919ce to your computer and use it in GitHub Desktop.
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