JuliaStrings / NaturalSort.jl
 1 ```module NaturalSort ``` 2 ```export natural ``` 3 4 ```function natural(x::AbstractString, y::AbstractString) ``` 5 7 ``` iterx = iterate(x) ``` 6 5 ``` itery = iterate(y) ``` 7 7 ``` while iterx !== nothing && itery !== nothing ``` 8 7 ``` cx, statex = iterx ``` 9 7 ``` cy, statey = itery ``` 10 7 ``` if isnumeric(cx) && isnumeric(cy) ``` 11 ``` # Skip leading zeros ``` 12 7 ``` while cx == '0' && (iterx = iterate(x, statex)) !== nothing ``` 13 7 ``` cx, statex = iterx ``` 14 ``` end ``` 15 7 ``` while cy == '0' && (itery = iterate(y, statey)) !== nothing ``` 16 7 ``` cy, statey = itery ``` 17 ``` end ``` 18 7 ``` if iterx === nothing || itery === nothing ``` 19 7 ``` break ``` 20 ``` end ``` 21 22 ``` # Begin comparing numbers ``` 23 2 ``` diff = false ``` 24 2 ``` lt = false ``` 25 7 ``` while true ``` 26 7 ``` isnumx = isnumeric(cx) ``` 27 7 ``` isnumy = isnumeric(cy) ``` 28 7 ``` if isnumx && isnumy ``` 29 7 ``` if !diff && cx != cy ``` 30 ``` # Keep track of how numbers differ, in case the lengths match ``` 31 2 ``` diff = true ``` 32 5 ``` lt = cx < cy ``` 33 ``` end ``` 34 35 7 ``` iterx = iterate(x, statex) ``` 36 7 ``` itery = iterate(y, statey) ``` 37 7 ``` if iterx === nothing || itery === nothing ``` 38 7 ``` if iterx === nothing && itery !== nothing && isnumeric(itery) ``` 39 ``` # Number in y is longer than number in x ``` 40 7 ``` return true ``` 41 7 ``` elseif iterx !== nothing && itery === nothing && isnumeric(iterx) ``` 42 ``` # Number in x is longer than number in y ``` 43 7 ``` return false ``` 44 ``` end ``` 45 ``` # Both numbers ended and same length ``` 46 7 ``` return diff ? lt : iterx === nothing && itery !== nothing ``` 47 ``` end ``` 48 49 7 ``` cx, statex = iterx ``` 50 7 ``` cy, statey = itery ``` 51 7 ``` elseif isnumx ``` 52 ``` # Number in x is longer than number in y ``` 53 7 ``` return false ``` 54 7 ``` elseif isnumy ``` 55 ``` # Number in y is longer than number in x ``` 56 7 ``` return true ``` 57 7 ``` elseif diff ``` 58 ``` # Numbers were same length but different ``` 59 7 ``` return lt ``` 60 ``` else ``` 61 ``` # Numbers were the same ``` 62 7 ``` break ``` 63 ``` end ``` 64 ``` end ``` 65 ``` end ``` 66 7 ``` if cx != cy ``` 67 7 ``` return cx < cy ``` 68 ``` end ``` 69 7 ``` iterx = iterate(x, statex) ``` 70 7 ``` itery = iterate(y, statey) ``` 71 ``` end ``` 72 73 7 ``` return iterx == nothing && itery != nothing ``` 74 ```end ``` 75 ```end # module ```

Read our documentation on viewing source code .

Loading