How to resolve the algorithm Natural sorting step by step in the J programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Natural sorting step by step in the J programming language
Table of Contents
Problem Statement
Natural sorting is the sorting of text that does more than rely on the order of individual characters codes to make the finding of individual strings easier for a human reader. There is no "one true way" to do this, but for the purpose of this task 'natural' orderings might include:
Note: it is not necessary to have individual control of which features are active in the natural sorting routine at any time.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Natural sorting step by step in the J programming language
Source code in the j programming language
require'strings regex'
lines=: <;.2
titleFix=: ('^\s*(the|a|an)\b';'')&rxrplc
isNum=: e.&'0123456789'
num=: ".^:(isNum@{.)
split=: <@num/.~ [:+/\1,2 ~:/\ isNum
norm=: [: split (32 9 12 13 14 15{a.) -.~ [: titleFix tolower
natSor=: lines ;@/: norm&.>@lines
IgnoringLeadingSpaces=:0 :0
ignore leading spaces: 2-2
ignore leading spaces: 2-1
ignore leading spaces: 2+0
ignore leading spaces: 2+1
)
IgnoringMultipleAdjacentSpaces=: 0 :0
ignore m.a.s spaces: 2-2
ignore m.a.s spaces: 2-1
ignore m.a.s spaces: 2+0
ignore m.a.s spaces: 2+1
)
bsSubst=: rplc&((LF;'\'),('\r';13{a.),('\x0c';12{a.),('\x0b';11{a.),('\n';LF),:'\t';TAB)
EquivalentWhitespaceCharacters=: bsSubst 0 :0
Equiv. spaces: 3-3
Equiv.\rspaces: 3-2
Equiv.\x0cspaces: 3-1
Equiv.\x0bspaces: 3+0
Equiv.\nspaces: 3+1
Equiv.\tspaces: 3+2
)
CaseIndepenent=: 0 :0
cASE INDEPENENT: 3-2
caSE INDEPENENT: 3-1
casE INDEPENENT: 3+0
case INDEPENENT: 3+1
)
NumericFieldsAsNumerics=: 0 :0
foo100bar99baz0.txt
foo100bar10baz0.txt
foo1000bar99baz10.txt
foo1000bar99baz9.txt
)
Titles=: 0 :0
The Wind in the Willows
The 40th step more
The 39 steps
Wanda
)
natSor IgnoringLeadingSpaces
ignore leading spaces: 2+0
ignore leading spaces: 2+1
ignore leading spaces: 2-1
ignore leading spaces: 2-2
natSor IgnoringMultipleAdjacentSpaces
ignore m.a.s spaces: 2+0
ignore m.a.s spaces: 2+1
ignore m.a.s spaces: 2-1
ignore m.a.s spaces: 2-2
natSor EquivalentWhitespaceCharacters
Equiv.
spaces: 3+1\ Equiv.�spaces: 3+0\ Equiv. spaces: 3+2\ Equiv.�spaces: 3-1\ Equiv.
spaces: 3-2\ Equiv. spaces: 3-3\
natSor CaseIndepenent
casE INDEPENENT: 3+0
case INDEPENENT: 3+1
caSE INDEPENENT: 3-1
cASE INDEPENENT: 3-2
natSor NumericFieldsAsNumerics
foo100bar10baz0.txt
foo100bar99baz0.txt
foo1000bar99baz9.txt
foo1000bar99baz10.txt
natSor Titles
The 39 steps
The 40th step more
Wanda
The Wind in the Willows
You may also check:How to resolve the algorithm Product of min and max prime factors step by step in the Draco programming language
You may also check:How to resolve the algorithm Binary strings step by step in the Haskell programming language
You may also check:How to resolve the algorithm Percolation/Mean cluster density step by step in the Nim programming language
You may also check:How to resolve the algorithm Metallic ratios step by step in the Ruby programming language
You may also check:How to resolve the algorithm Hello world/Newbie step by step in the Groovy programming language