Sort an outline at every level: Difference between revisions

Content added Content deleted
(J draft)
m (syntax highlighting fixup automation)
Line 117: Line 117:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>Sort_an_outline(data, reverse:=""){
<syntaxhighlight lang="autohotkey">Sort_an_outline(data, reverse:=""){
;-----------------------
;-----------------------
; get Delim, Error Check
; get Delim, Error Check
Line 169: Line 169:
result.=delim
result.=delim
return result
return result
}</lang>
}</syntaxhighlight>
Examples: "Example of Data_2"<lang AutoHotkey>Data_2 =
Examples: "Example of Data_2"<syntaxhighlight lang="autohotkey">Data_2 =
(
(
zeta
zeta
Line 186: Line 186:
MsgBox % Sort_an_outline(Data_2)
MsgBox % Sort_an_outline(Data_2)
MsgBox % Sort_an_outline(Data_2, 1)
MsgBox % Sort_an_outline(Data_2, 1)
return</lang>
return</syntaxhighlight>
Output: tabulated for ease of reading, actual output is text only, Error check returns first line with inconsistent delimiter!<pre>
Output: tabulated for ease of reading, actual output is text only, Error check returns first line with inconsistent delimiter!<pre>
======================================================================================================
======================================================================================================
Line 225: Line 225:
=={{header|Go}}==
=={{header|Go}}==
{{trans|Wren}}
{{trans|Wren}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 416: Line 416:
fmt.Println("\nSecond unspecified outline, descending sort:")
fmt.Println("\nSecond unspecified outline, descending sort:")
sortedOutline(outline4, false)
sortedOutline(outline4, false)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 536: Line 536:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>{-# LANGUAGE OverloadedStrings #-}
<syntaxhighlight lang="haskell">{-# LANGUAGE OverloadedStrings #-}


import Data.Tree (Tree(..), foldTree)
import Data.Tree (Tree(..), foldTree)
Line 692: Line 692:
| x <- textLines
| x <- textLines
, s <- [T.takeWhile isSpace x]
, s <- [T.takeWhile isSpace x]
, 0 /= T.length s ]</lang>
, 0 /= T.length s ]</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Four-spaced (A -> Z):
<pre>Four-spaced (A -> Z):
Line 771: Line 771:


=={{header|J}}==
=={{header|J}}==
Implementation:<lang J>parse2=: {{
Implementation:<syntaxhighlight lang="j">parse2=: {{
((=<./)y (1 i.~ -.@e.)S:0 m) m {{
((=<./)y (1 i.~ -.@e.)S:0 m) m {{
({.y),<m parse2^:(*@#)}.y
({.y),<m parse2^:(*@#)}.y
Line 793: Line 793:
sortout=: {{ if. L.y do. u~ ({."1 y),.u sortout each}."1 y else. u~y end. }}
sortout=: {{ if. L.y do. u~ ({."1 y),.u sortout each}."1 y else. u~y end. }}


unparse=: {{ ;<S:0 y }}</lang>
unparse=: {{ ;<S:0 y }}</syntaxhighlight>


Task examples (task data defined below):<lang J> /:sortout&.parseout sample1
Task examples (task data defined below):<syntaxhighlight lang="j"> /:sortout&.parseout sample1
alpha
alpha
epsilon
epsilon
Line 857: Line 857:
gamma
gamma
delta
delta
</syntaxhighlight>
</lang>


Data for task examples:
Data for task examples:
<lang J>sample1=: {{)n
<syntaxhighlight lang="j">sample1=: {{)n
zeta
zeta
beta
beta
Line 915: Line 915:
iota
iota
epsilon
epsilon
}}</lang>
}}</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
A <code>for</code> loop was used in the constructor, and recursive functions for sorting and printing.
A <code>for</code> loop was used in the constructor, and recursive functions for sorting and printing.
<lang julia>import Base.print
<syntaxhighlight lang="julia">import Base.print


abstract type Entry end
abstract type Entry end
Line 1,072: Line 1,072:
println(y)
println(y)
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Given the text:
Given the text:
Line 1,182: Line 1,182:
There are several differences between the Julia original and our transcription. Most are due to the fact that Nim way to do some things is different of the Julia way to do it.
There are several differences between the Julia original and our transcription. Most are due to the fact that Nim way to do some things is different of the Julia way to do it.


<lang Nim>import algorithm, sequtils, strformat, strutils
<syntaxhighlight lang="nim">import algorithm, sequtils, strformat, strutils


type
type
Line 1,346: Line 1,346:
epsilon""")
epsilon""")
except ValueError:
except ValueError:
echo getCurrentExceptionMsg()</lang>
echo getCurrentExceptionMsg()</syntaxhighlight>


{{out}}
{{out}}
Line 1,453: Line 1,453:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict; # https://rosettacode.org/wiki/Sort_an_outline_at_every_level
use strict; # https://rosettacode.org/wiki/Sort_an_outline_at_every_level
Line 1,538: Line 1,538:
theta
theta
iota
iota
epsilon</lang>
epsilon</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,636: Line 1,636:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(notonline)-->
<!--<syntaxhighlight lang="phix">(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">javascript_semantics</span> <span style="color: #000080;font-style:italic;">-- (tab chars are browser kryptonite)</span>
<span style="color: #008080;">without</span> <span style="color: #008080;">javascript_semantics</span> <span style="color: #000080;font-style:italic;">-- (tab chars are browser kryptonite)</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">print_children</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">children</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">indent</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">bRev</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">print_children</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">children</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">indent</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">bRev</span><span style="color: #0000FF;">)</span>
Line 1,718: Line 1,718:
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,783: Line 1,783:


=={{header|Python}}==
=={{header|Python}}==
<lang python>'''Sort an outline at every level'''
<syntaxhighlight lang="python">'''Sort an outline at every level'''




Line 2,199: Line 2,199:
# MAIN ---
# MAIN ---
if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
{{Out}}
{{Out}}
<pre>4-space indented (A -> Z):
<pre>4-space indented (A -> Z):
Line 2,278: Line 2,278:
* Routine(s) to output the data structure in the desire sort order.
* Routine(s) to output the data structure in the desire sort order.


<lang perl6>my @tests = q:to/END/.split( /\n\n+/ )».trim;
<syntaxhighlight lang="raku" line>my @tests = q:to/END/.split( /\n\n+/ )».trim;
zeta
zeta
beta
beta
Line 2,383: Line 2,383:
multi pretty-print (Str $struct, :$level = 0, :$ws = ' ', :$desc = False) {
multi pretty-print (Str $struct, :$level = 0, :$ws = ' ', :$desc = False) {
say $ws x $level , $struct;
say $ws x $level , $struct;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>=======================================================
<pre>=======================================================
Line 2,497: Line 2,497:
{{libheader|Wren-sort}}
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/sort" for Sort
<syntaxhighlight lang="ecmascript">import "/sort" for Sort
import "/fmt" for Fmt
import "/fmt" for Fmt


Line 2,651: Line 2,651:


System.print("\nSecond unspecified outline, descending sort:")
System.print("\nSecond unspecified outline, descending sort:")
sortedOutline.call(outline4, false)</lang>
sortedOutline.call(outline4, false)</syntaxhighlight>


{{out}}
{{out}}