Decorate-sort-undecorate idiom: Difference between revisions

Content added Content deleted
(→‎{{header|C}}: Should really store the sorted array.)
Line 705: Line 705:


More complicated transforms may ''require'' an explicit schwartzian transform routine. An example of where an explicit transform is desirable is the <code>schwartzian()</code> routine in the [[P-value_correction#Raku|Raku entry for the P-value_correction task]].
More complicated transforms may ''require'' an explicit schwartzian transform routine. An example of where an explicit transform is desirable is the <code>schwartzian()</code> routine in the [[P-value_correction#Raku|Raku entry for the P-value_correction task]].
=={{header|Ruby}}==

Arrays have a <code>sort_by</code> method which does a Schwartzian transform.
<syntaxhighlight lang="ruby">p "Rosetta Code is a programming chrestomathy site".split.sort_by(&:size)
# sort by word-size, then lexical:
str = "Rosetta Code is a programming chrestomathy site seven extra words added to this demo"
p str.split.sort_by{|word| [word.size, word]
</syntaxhighlight>
{{out}}
<pre>["a", "is", "Code", "site", "Rosetta", "programming", "chrestomathy"]
["a", "is", "to", "Code", "demo", "site", "this", "added", "extra", "seven", "words", "Rosetta", "programming", "chrestomathy"]
</pre>


=={{header|Rust}}==
=={{header|Rust}}==