Skip to content

Commit 56aa494

Browse files
author
livecodeali
committed
[[ Sort ]] Finish implementing and documenting sort using handler
1 parent af86f5e commit 56aa494

1 file changed

Lines changed: 55 additions & 2 deletions

File tree

libscript/src/sort.mlc

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,61 @@ end syntax
245245

246246
--
247247

248-
public handler type SortHandler(in pLeft as any, in pRight as any) returns integer
249-
public foreign handler MCSortExecSortListUsingHandler(inout Target as List, in Handler as SortHandler) returns nothing binds to "<builtin>"
248+
/*
249+
Summary: A handler type that can be used in the <SortListUsingHandler> command.
250+
251+
Parameters:
252+
pLeft: The left hand value to compare.
253+
pRight: The right hand value to compare.
254+
255+
Returns:
256+
An integer less than 0 if pLeft is less than pRight with respect to the intended ordering,
257+
an integer greater than 0 if pLeft is greater than pRight with respect to the intended ordering,
258+
or 0 if pLeft is equal to pRight.
259+
260+
Description:
261+
Any handler of type <SortCompare> can be passed to <SortListUsingHandler> to sort a list.
262+
It takes two arguments, and returns an integer based on the comparison.
263+
264+
Tags: Sorting
265+
*/
266+
public handler type SortCompare(in pLeft as any, in pRight as any) returns Integer
267+
268+
public foreign handler MCSortExecSortListUsingHandler(inout Target as List, in Handler as SortCompare) returns nothing binds to "<builtin>"
269+
270+
/*
271+
Summary: Sorts <Target> using <Handler> as a comparison function.
272+
Target: An expression that evaluates to a list.
273+
Handler: A handler of type <SortCompare>
274+
275+
Example:
276+
handler RandomSort(in pLeft as any, in pRight as any) returns Integer
277+
if any number > 0.5 then
278+
return 1
279+
else
280+
return -1
281+
end if
282+
end handler
283+
284+
public handler RandomlySorted() returns List
285+
variable tList as List
286+
put [] into tList
287+
288+
variable tCount as Number
289+
repeat with tCount from 1 up to 1000
290+
push tCount onto tList
291+
end repeat
292+
293+
sort tList using handler RandomSort
294+
return tList
295+
end handler
296+
297+
Description:
298+
<SortListUsingHandler> sorts a list by comparing the elements of a list according to the
299+
comparison implemented by the <Handler> argument.
300+
301+
Tags: Sorting
302+
*/
250303

251304
syntax SortListUsingHandler is statement
252305
"sort" <Target: Expression> "using" <Handler: Expression>

0 commit comments

Comments
 (0)