Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added List.sort(comp) to List module #802

Merged
merged 14 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from 12 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
19 changes: 19 additions & 0 deletions doc/site/modules/core/list.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ System.print(["a", "b", "c"].removeAt(1)) //> b

It is a runtime error if the index is not an integer or is out of bounds.

### **sort**(), **sort**(comparer)

Sorts the elements of a list in-place; altering the list.

<pre class="snippet">
var list = [4, 1, 3, 2].sort()
System.print(list) //> [1, 2, 3, 4]
</pre>

A comparison function `comparer` can be provided to customise the element sorting. The comparison function must return a boolean value specifying the order in which elements should appear in the list. The default behaviour uses a `<` comparison.
mathewmariani marked this conversation as resolved.
Show resolved Hide resolved

<pre class="snippet">
var list = [9, 6, 8, 7]
list.sort {|a, b| a < b}
System.print(list) //> [6, 7, 8, 9]
</pre>

It is a runtime error if `comparer` is not a function.

### **[**index**]** operator

Gets the element at `index`. If `index` is negative, it counts backwards from
Expand Down
35 changes: 35 additions & 0 deletions src/vm/wren_core.wren
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,41 @@ class List is Sequence {
return other
}

sort() { sort {|a, b| a < b } }
mathewmariani marked this conversation as resolved.
Show resolved Hide resolved

sort(comparer) {
if (!(comparer is Fn)) {
Fiber.abort("Comparer must be a function.")
}
quicksort_(0, count - 1, comparer)
return this
}

quicksort_(a, b, comparer) {
if (a < b) {
mathewmariani marked this conversation as resolved.
Show resolved Hide resolved
var p = partition_(a, b, comparer)
quicksort_(a, p - 1, comparer)
quicksort_(p + 1, b, comparer)
}
}

partition_(a, b, comparer) {
var p = this[b]
var i = a - 1
for (j in a..(b-1)) {
if (comparer.call(this[j], p)) {
i = i + 1
var t = this[i]
this[i] = this[j]
this[j] = t
}
}
var t = this[i+1]
this[i+1] = this[b]
this[b] = t
return i+1
}

toString { "[%(join(", "))]" }

+(other) {
Expand Down
35 changes: 35 additions & 0 deletions src/vm/wren_core.wren.inc
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,41 @@ static const char* coreModuleSource =
" return other\n"
" }\n"
"\n"
" sort() { sort {|a, b| a < b } }\n"
"\n"
" sort(comparer) {\n"
" if (!(comparer is Fn)) {\n"
" Fiber.abort(\"Comparer must be a function.\")\n"
" }\n"
" quicksort_(0, count - 1, comparer)\n"
" return this\n"
" }\n"
"\n"
" quicksort_(a, b, comparer) {\n"
" if (a < b) {\n"
" var p = partition_(a, b, comparer)\n"
" quicksort_(a, p - 1, comparer)\n"
" quicksort_(p + 1, b, comparer)\n"
" }\n"
" }\n"
"\n"
" partition_(a, b, comparer) {\n"
" var p = this[b]\n"
" var i = a - 1\n"
" for (j in a..(b-1)) {\n"
" if (comparer.call(this[j], p)) { \n"
" i = i + 1\n"
" var t = this[i]\n"
" this[i] = this[j]\n"
" this[j] = t\n"
" }\n"
" }\n"
" var t = this[i+1]\n"
" this[i+1] = this[b]\n"
" this[b] = t\n"
" return i+1\n"
" }\n"
"\n"
" toString { \"[%(join(\", \"))]\" }\n"
"\n"
" +(other) {\n"
Expand Down
9 changes: 9 additions & 0 deletions test/core/list/sort.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
System.print([4, 1, 3, 2].sort()) // expect: [1, 2, 3, 4]
mathewmariani marked this conversation as resolved.
Show resolved Hide resolved

var l = [10, 7, 8, 9, 1, 5]
l.sort{|a, b| a < b }
System.print(l) // expect: [1, 5, 7, 8, 9, 10]
l.sort{|a, b| a > b }
System.print(l) // expect: [10, 9, 8, 7, 5, 1]

[10, 7, 8, 9, 1, 5].sort(3) // expect runtime error: Comparer must be a function.