Skip to content

Instantly share code, notes, and snippets.

@cossio
Last active August 14, 2017 10:15
Show Gist options
  • Save cossio/a73e82fd69e3ce49c0ec1da4e6a2372d to your computer and use it in GitHub Desktop.
Save cossio/a73e82fd69e3ce49c0ec1da4e6a2372d to your computer and use it in GitHub Desktop.
Sort strings in natural order (numbers are inside are treated by value).
# done by Sebastian Pfitzner
function naturalsort(x::Vector{String})
f = text -> all(isnumber, text) ? string(Char(parse(Int, text))) : text
sorter = key -> join(f(c) for c in matchall(r"[0-9]+|[^0-9]+", key))
sort(x, by=sorter)
end
# see https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment