Sort using a custom comparator: Difference between revisions

Content added Content deleted
m (→‎{{header|AppleScript}}: →‎Vanilla: New sort handler URL, tidy-up.)
(adding lambdatalk)
Line 2,632: Line 2,632:
<pre>Unsorted: [Here, are, some, sample, strings, to, be, sorted]
<pre>Unsorted: [Here, are, some, sample, strings, to, be, sorted]
Sorted: [strings, sample, sorted, Here, some, are, be, to]</pre>
Sorted: [strings, sample, sorted, Here, some, are, be, to]</pre>

=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">

{def sortbylength

{def sortbylength.i
{lambda {:x :a}
{if {A.empty? :a}
then {A.new :x}
else {if {> {W.length :x} {W.length {A.first :a}}}
then {A.addfirst! :x :a}
else {A.addfirst! {A.first :a}
{sortbylength.i :x {A.rest :a}}} }}}}

{def sortbylength.r
{lambda {:a1 :a2}
{if {A.empty? :a1}
then :a2
else {sortbylength.r {A.rest :a1}
{sortbylength.i {A.first :a1} :a2}} }}}

{lambda {:s}
{S.replace (\[|\]) by in
{S.replace , by space in
{A.disp {sortbylength.r {A.new :s} {A.new}} }}}}}
-> sortbylength

{sortbylength here are Some sample strings to be sorted}
-> strings sample sorted here Some are to be
</syntaxhighlight>


=={{header|Lua}}==
=={{header|Lua}}==