Skip to content

Commit a64f186

Browse files
authored
issorted(ld; byvalue=true) for LittleDicts (#151)
Since `byvalue` is a keyword for `sort`, we should support it for `issorted`. Otherwise, you can't check whether a LittleDict is sorted for generic kwargs, or without diving into internals.
1 parent 5ec1764 commit a64f186

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/dict_sorting.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Sort for dicts
2-
import Base: sort, sort!
2+
import Base: sort, sort!, issorted
33

44
function sort!(d::OrderedDict; byvalue::Bool=false, args...)
55
if d.ndel > 0
@@ -55,3 +55,10 @@ function sort(d::LittleDict; byvalue::Bool=false, args...)
5555
return LittleDict(d.keys[p], d.vals[p])
5656
end
5757

58+
function issorted(d::LittleDict; byvalue::Bool=false, args...)
59+
if byvalue
60+
return issorted(d.vals; args...)
61+
else
62+
return issorted(d.keys; args...)
63+
end
64+
end

test/test_little_dict.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,9 @@ using OrderedCollections: FrozenLittleDict, UnfrozenLittleDict
508508
@test collect(values(sd)) == collect('z':-1:'q')
509509
@test sort(sd) == sd
510510

511+
@test !issorted(d; byvalue=true)
511512
sdv = sort(d; byvalue=true)
513+
@test issorted(sdv; byvalue=true)
512514
@test collect(keys(d)) == ks # verify d is not changed by sort()
513515
@test collect(keys(sdv)) == 10:-1:1
514516
@test collect(values(sdv)) == collect('q':'z')

0 commit comments

Comments
 (0)