Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
roqua-support
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
RoQua
roqua-support
Commits
6a04eaaa
Commit
6a04eaaa
authored
3 years ago
by
Henk
Browse files
Options
Downloads
Patches
Plain Diff
Make sort_alphanum consistent for non-alphanum as well
parent
cddadadd
No related branches found
Branches containing commit
Tags
v0.1.15
Tags containing commit
1 merge request
!47
Make sort_by_alphanum stable
Pipeline
#100247
passed
3 years ago
Stage: test
Changes
2
Pipelines
4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/roqua/core_ext/enumerable/sort_by_alphanum.rb
+34
-18
34 additions, 18 deletions
lib/roqua/core_ext/enumerable/sort_by_alphanum.rb
spec/roqua/core_ext/enumerable/sort_by_alphanum_spec.rb
+5
-0
5 additions, 0 deletions
spec/roqua/core_ext/enumerable/sort_by_alphanum_spec.rb
with
39 additions
and
18 deletions
lib/roqua/core_ext/enumerable/sort_by_alphanum.rb
+
34
−
18
View file @
6a04eaaa
...
...
@@ -21,29 +21,45 @@ module Enumerable
private
def
grouped_compare
(
a
,
b
)
loop
{
a_chunk
,
a
=
extract_alpha_or_number_group
(
a
)
b_chunk
,
b
=
extract_alpha_or_number_group
(
b
)
ALL_NUM
=
/\d+/
ALL_ALPHA
=
/[A-Za-z]+/
NON_ALPHANUM
=
/[^A-Za-z0-9]+/
ret
=
if
a_chunk
=~
/\d/
&&
b_chunk
=~
/\d/
&&
a_chunk
.
to_i
!=
b_chunk
.
to_i
def
grouped_compare
(
a
,
b
)
a_scanner
=
StringScanner
.
new
(
a
)
b_scanner
=
StringScanner
.
new
(
b
)
# each loop has to do exactly 1 non-nil-scan on both scanners or return a non-zero value.
loop
do
ret
=
\
if
a_scanner
.
eos?
-
1
elsif
(
a_chunk
=
a_scanner
.
scan
(
ALL_NUM
))
if
(
b_chunk
=
b_scanner
.
scan
(
ALL_NUM
))
if
a_chunk
.
to_i
!=
b_chunk
.
to_i
a_chunk
.
to_i
<=>
b_chunk
.
to_i
else
else
# 03 vs 3
a_chunk
<=>
b_chunk
end
return
-
1
if
a_chunk
==
''
elsif
b_scanner
.
scan
(
ALL_ALPHA
)
-
1
else
# NON_ALPHANUM
1
end
elsif
(
a_chunk
=
a_scanner
.
scan
(
ALL_ALPHA
))
if
(
b_chunk
=
b_scanner
.
scan
(
ALL_ALPHA
))
a_chunk
<=>
b_chunk
else
# ALL_NUM or NON_ALPHANUM
1
end
else
# NON_ALPHANUM
a_chunk
=
a_scanner
.
scan
(
NON_ALPHANUM
)
if
(
b_chunk
=
b_scanner
.
scan
(
NON_ALPHANUM
))
a_chunk
<=>
b_chunk
else
-
1
end
end
return
ret
if
ret
!=
0
}
end
def
extract_alpha_or_number_group
(
item
)
matchdata
=
/([A-Za-z]+|[\d]+)/
.
match
(
item
)
if
matchdata
.
nil?
[
""
,
""
]
else
[
matchdata
[
0
],
item
=
item
[
matchdata
.
offset
(
0
)[
1
]
..
-
1
]]
end
end
end
This diff is collapsed.
Click to expand it.
spec/roqua/core_ext/enumerable/sort_by_alphanum_spec.rb
+
5
−
0
View file @
6a04eaaa
...
...
@@ -11,6 +11,11 @@ describe Enumerable do
expect
(
input
.
sort_by_alphanum
(
&
:reverse
)).
to
eq
[
"004some10thing"
,
"004some11thing"
,
"3another"
]
end
it
'treats non-alphanum as lower than alpha and num'
do
input
=
%w[b3a b{c bԘb]
# curlies are above alpha in utf-8, Ԙ is multi-byte
expect
(
input
.
sort_by_alphanum
).
to
eq
%w[b{c bԘb b3a]
end
it
'compares number chunks as integers'
do
expect
(
%w(004 3)
.
sort_by_alphanum
).
to
eq
%w(3 004)
end
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment