stdsort - anssihalmeaho/funl GitHub Wiki

stdsort

Provides services for sorting lists.

Services

sort

Sorts list given as 1st argument.

By default values in list are assumed to be int's or float's (can have both int's and float's in same list). Sorted order is such that smaller values are first.

Optionally 2nd argument can be give to provide comparison function. If it's given then that is used for comparison (instead of lt operator)

Comparison function format is:

func(<value-1> <value-2>) -> bool

If comparison function returns true then first value is assumed to be first in order, if it returns false then second value is assumed to be first.

Implementation of sort uses mergesort algorithm.

Format:

call(stdsort.sort <list>) -> <list>
call(stdsort.sort <list> <optional: comparison-func>) -> <list>

Return value is sorted list.

Example

Example: sorting int/float list

ns main

main = func()
	import stdsort

	l = list(11 0.25 15 5.0 4 6 10)
	call(stdsort.sort l)
end

endns

Output is:

list(0.25, 4, 5.0, 6, 10, 11, 15)

Example: sorting with own comparator function

In this example strings are sorted by length of strings.

ns main

main = func()
	import stdsort

	l = list('these' 'are' 'some' 'strings')

	# order by length of strings
	compa = func(a b)
		astr = str(a)
		bstr = str(b)
		lt(len(astr) len(bstr))
	end

	call(stdsort.sort l compa)
end

endns

Output is:

list('are', 'some', 'these', 'strings')
⚠️ **GitHub.com Fallback** ⚠️