Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions docs/lexicon.html
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,13 @@ <h2 class="title-2" id="python-builtins">
<code class="code"
><span class="fn-name">sorted</span><span class="fn-p">(</span
><span class="fn-arg">seq</span>[,<span class="fn-arg">key</span
>]<span class="fn-p">)</span></code
>][,<span class="fn-arg">reverse</span>]<span class="fn-p"
>)</span></code
>
- returns a copy of <code class="code">seq</code> with the contents
sorted. <code class="code">key</code> is a function that is applied
to each item before comparison.
to each item before comparison. If <code class="code">reverse</code
> is true, the items are sorted in reverse order.
</span>
</li>
<li>
Expand Down
2 changes: 1 addition & 1 deletion rules/builtins.build_defs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def glob(include:list|str, exclude:list|str&excludes=[], hidden:bool=CONFIG.BAZE
def package():
pass

def sorted(seq:list, key:function=None) -> list:
def sorted(seq:list, key:function=None, reverse:bool=False) -> list:
pass

def reversed(seq:list) -> list:
Expand Down
10 changes: 8 additions & 2 deletions src/parse/asp/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,11 +800,17 @@ func dictCopy(s *scope, args []pyObject) pyObject {
func sorted(s *scope, args []pyObject) pyObject {
l, isList := args[0].(pyList)
key, isFunc := args[1].(*pyFunc)
reverse, isBool := args[2].(pyBool)
s.Assert(isList, "Argument seq must be a list, not %s", args[0].Type())
s.Assert(isBool, "Argument reverse must be a bool, not %s", args[2].Type())
order := LessThan
if reverse {
order = GreaterThan
}
l = l[:]
if key == nil {
sort.Slice(l, func(i, j int) bool {
return s.operator(LessThan, l[i], l[j]).IsTruthy()
return s.operator(order, l[i], l[j]).IsTruthy()
})
} else {
s.Assert(isFunc, "Argument key must be callable, not %s", args[1].Type())
Expand All @@ -819,7 +825,7 @@ func sorted(s *scope, args []pyObject) pyObject {
Value: Expression{optimised: &optimisedExpression{Constant: l[j]}},
}},
})
return s.operator(LessThan, iKey, jKey).IsTruthy()
return s.operator(order, iKey, jKey).IsTruthy()
})
}
return l
Expand Down
2 changes: 2 additions & 0 deletions src/parse/asp/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ func TestInterpreterSorting(t *testing.T) {
// N.B. sorted() sorts in-place, unlike Python's one. We may change that later.
assert.Equal(t, pyList{pyInt(1), pyInt(2), pyInt(3)}, s.Lookup("r1"))
assert.Equal(t, pyList{pyString("ONE"), pyString("THREE"), pyString("two")}, s.Lookup("r2"))
assert.Equal(t, pyList{pyInt(4), pyInt(3), pyInt(2), pyInt(1)}, s.Lookup("r3"))
assert.Equal(t, pyList{pyInt(1), pyInt(2), pyInt(3), pyInt(4)}, s.Lookup("r4"))
}

func TestReversed(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions src/parse/asp/test_data/interpreter/sorted.build
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ r1 = sorted(l1)
# key parameter test
l2 = ["ONE", "two", "THREE"]
r2 = sorted(l2, key=lambda s: s.lower())

# reverse parameter test
r3 = sorted([2, 4, 1, 3], reverse=True)
r4 = sorted([2, 4, 1, 3], reverse=False)
Loading