JortSort: Difference between revisions

21,061 bytes added ,  29 days ago
Added Easylang
No edit summary
(Added Easylang)
 
(45 intermediate revisions by 31 users not shown)
Line 1:
{{task|Sorting Algorithms}}
{{Sorting Algorithm}}
[[Category:Sorting]]
 
{{noticebox||Note:   jortSort is considered a work of satire.   It achieves its result in an intentionally roundabout way.   You are encouraged to write your solutions in the spirit of the original jortsort rather than trying to give the most concise or idiomatic solution.}}
jortSort is a sorting toolset that makes the user do the work and guarantees efficiency because you don't have to sort ever again. It was originally presented by Jenn "Moneydollars" Schiffer at the prestigious [https://www.youtube.com/watch?v=pj4U_W0OFoE JSConf].
 
 
jortSort is a function that takes a single array of comparable objects as its argument. It then sorts the array in ascending order and compares the sorted array to the originally provided array. If the arrays match (i.e. the original array was already sorted), the function returns true. If the arrays do not match (i.e. the original array was not sorted), the function returns false.
JortSort is a sorting tool set that makes the user do the work and guarantees efficiency because you don't have to sort ever again.
<br>It was originally presented by Jenn "Moneydollars" Schiffer at the
prestigious &nbsp; [https://www.youtube.com/watch?v=pj4U_W0OFoE JSConf].
 
 
JortSort is a function that takes a single array of comparable objects as its argument.
 
It then sorts the array in ascending order and compares the sorted array to the originally provided array.
 
If the arrays match &nbsp; (i.e. the original array was already sorted), &nbsp; the function returns &nbsp; '''true'''.
 
If the arrays do not match (i.e. the original array was not sorted), the function returns &nbsp; '''false'''.
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F jortsort(sequence)
R Array(sequence) == sorted(sequence)
 
F print_for_seq(seq)
print(‘jortsort(#.) is #.’.format(seq, jortsort(seq)))
 
print_for_seq([1, 2, 4, 3])
print_for_seq([14, 6, 8])
print_for_seq([‘a’, ‘c’])
print_for_seq(‘CVGH’)
print_for_seq(‘PQRST’)</syntaxhighlight>
 
{{out}}
<pre>
jortsort([1, 2, 4, 3]) is 0B
jortsort([14, 6, 8]) is 0B
jortsort([a, c]) is 1B
jortsort(CVGH) is 0B
jortsort(PQRST) is 1B
</pre>
 
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program jortSort64.s */
 
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
 
/*******************************************/
/* Initialized data */
/*******************************************/
.data
szMessOk: .asciz "Ok, the list is sorted. \n"
szMessNotOk: .asciz "Ouah!! this list is unsorted.\n"
szCarriageReturn: .asciz "\n"
tbNumber: .quad 3
.quad 4
.quad 20
.quad 5
.equ LGTBNUMBER, (. - tbNumber)/8 // number element of area
/*******************************************/
/* UnInitialized data */
/*******************************************/
.bss
sZoneConversion: .skip 100
.align 4
tbNumberSorted: .skip 8 * LGTBNUMBER
/*******************************************/
/* code section */
/*******************************************/
.text
.global main
main: // entry of program
ldr x0,qAdrtbNumber
ldr x1,qAdrtbNumberSorted
mov x2,LGTBNUMBER
bl insertionSort // sort area
ldr x0,qAdrtbNumber
ldr x1,qAdrtbNumberSorted
mov x2,LGTBNUMBER
bl comparArea // control area
cbz x0,1f
ldr x0,qAdrszMessNotOk // not sorted
bl affichageMess
b 100f
1: // ok it is good
ldr x0,qAdrszMessOk
bl affichageMess
100: // standard end of the program
mov x0, #0 // return code
mov x8, #EXIT // request to exit program
svc 0 // perform the system call
 
qAdrtbNumber: .quad tbNumber
qAdrtbNumberSorted: .quad tbNumberSorted
qAdrszMessNotOk: .quad szMessNotOk
qAdrszMessOk: .quad szMessOk
qAdrszCarriageReturn: .quad szCarriageReturn
/******************************************************************/
/* insertion sort */
/******************************************************************/
/* x0 contains the address of area to sort */
/* x1 contains the address of area sorted */
/* x2 contains the number of element */
insertionSort:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
mov x3,0
1: // copy area unsorted to other area
ldr x4,[x0,x3,lsl 3]
str x4,[x1,x3,lsl 3]
add x3,x3,1
cmp x3,x2
blt 1b
 
mov x3,1 // and sort area
2:
ldr x4,[x1,x3,lsl 3]
subs x5,x3,1
3:
cbz x5,4f
ldr x6,[x1,x5,lsl 3]
cmp x6,x4
ble 4f
add x7,x5,1
str x6,[x1,x7,lsl 3]
subs x5,x5,1
b 3b
4:
add x5,x5,1
str x4,[x1,x5,lsl 3]
add x3,x3,1
cmp x3,x2
blt 2b
100:
ldp x2,x3,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret
/******************************************************************/
/* Comparaison elements of two areas */
/******************************************************************/
/* x0 contains the address of area to sort */
/* x1 contains the address of area sorted */
/* x2 contains the number of element */
comparArea:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
mov x3,0
1:
ldr x4,[x0,x3,lsl 3] // load element area 1
ldr x5,[x1,x3,lsl 3] // load element area 2
cmp x4,x5 // equal ?
bne 99f // no -> error
add x3,x3,1 // yes increment indice
cmp x3,x2 // maxi ?
blt 1b // no -> loop
mov x0,0 // yes -> it is ok
b 100f
99:
mov x0,1
100:
ldp x2,x3,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret
 
 
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">PROC PrintArray(INT ARRAY a INT size)
INT i
 
Put('[)
FOR i=0 TO size-1
DO
IF i>0 THEN Put(' ) FI
PrintI(a(i))
OD
Put('])
RETURN
 
PROC InsertionSort(INT ARRAY a INT size)
INT i,j,value
 
FOR i=1 TO size-1
DO
value=a(i)
j=i-1
WHILE j>=0 AND a(j)>value
DO
a(j+1)=a(j)
j==-1
OD
a(j+1)=value
OD
RETURN
 
BYTE FUNC IsSorted(INT ARRAY a INT n)
INT ARRAY b(20)
INT i
 
IF n=0 THEN
RETURN (1)
FI
 
MoveBlock(b,a,n*2)
InsertionSort(b,n)
FOR i=0 TO n-1
DO
IF b(i)#a(i) THEN
RETURN (0)
FI
OD
RETURN (1)
 
PROC Test(INT ARRAY a INT n)
BYTE sorted
 
sorted=IsSorted(a,n)
PrintArray(a,n)
IF sorted THEN
PrintE(" is sorted")
ELSE
PrintE(" is not sorted")
FI
RETURN
 
PROC Main()
INT ARRAY
a=[65530 0 1 2 10 13 16],
b=[2 3 6 4],
c=[3 3 3 3 3 3],
d=[7]
 
Test(a,7)
Test(b,4)
Test(c,6)
Test(d,1)
Test(d,0)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/JortSort.png Screenshot from Atari 8-bit computer]
<pre>
[-6 0 1 2 10 13 16] is sorted
[2 3 6 4] is not sorted
[3 3 3 3 3 3] is sorted
[7] is sorted
[] is sorted
</pre>
 
=={{header|Ada}}==
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO, Ada.Containers.Generic_Array_Sort;
 
procedure Jortsort is
Line 25 ⟶ 282:
Put_Line("""abbigail"" sorted: " & Boolean'Image(Jort_Sort("abbigail")));
Put_Line("""abbey"" sorted: " & Boolean'Image(Jort_Sort("abbey")));
end Jortsort;</langsyntaxhighlight>
 
{{out}}
Line 31 ⟶ 288:
<pre>"abbigail" sorted: FALSE
"abbey" sorted: TRUE</pre>
 
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
 
 
------------------------- JORTSORT -------------------------
 
 
-- jortSort :: Ord a => [a] -> Bool
on jortSort(xs)
xs = sort(xs)
end jortSort
 
 
--------------------------- TEST ---------------------------
on run
map(jortSort, {{4, 5, 1, 3, 2}, {1, 2, 3, 4, 5}})
--> {false, true}
end run
 
 
------------------------- GENERIC --------------------------
 
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
-- The list obtained by applying f
-- to each element of xs.
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
 
 
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
-- 2nd class handler function lifted into 1st class script wrapper.
if script is class of f then
f
else
script
property |λ| : f
end script
end if
end mReturn
 
 
-- sort :: Ord a => [a] -> [a]
on sort(xs)
((current application's NSArray's arrayWithArray:xs)'s ¬
sortedArrayUsingSelector:"compare:") as list
end sort</syntaxhighlight>
{{Out}}
<pre>{false, true}</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">jortSort?: function [a]->
a = sort a
 
test: function [a]->
print [a "is" (jortSort? a)? -> "sorted" -> "not sorted"]
 
test [1 2 3]
test [2 3 1]
 
print ""
 
test ["a" "b" "c"]
test ["c" "a" "b"]</syntaxhighlight>
 
{{out}}
 
<pre>[1 2 3] is sorted
[2 3 1] is not sorted
 
[a b c] is sorted
[c a b] is not sorted</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">JortSort(Array){
sorted:=[]
for index, val in Array
Line 41 ⟶ 383:
return 0
return 1
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">Array1 := ["a", "d", "b" , "c"]
Array2 := ["a", "b", "c" , "d"]
MsgBox % JortSort(Array1) "`n" JortSort(Array2)
return</langsyntaxhighlight>
Outputs:<pre>0
1</pre>
 
=={{header|BQN}}==
BQN has a really simple way to represent JortSort:
 
<syntaxhighlight lang="bqn"> JSort ← ≡⟜∧
JSort 3‿2‿1
0</syntaxhighlight>
Which means "Does the array match itself sorted?"
 
Another equivalent function is <code>⊢≡∧</code>.
 
 
 
=={{header|C}}==
Line 54 ⟶ 408:
As others did in this page, this example doesn't follow the request to sort the input array and then compare the sorted version to the original one to check if it was already sorted. It only checks if the input is sorted and behaves accordingly.
I've tested this code only with gcc but should work with any C compiler.
<syntaxhighlight lang="c">
<lang c>
#include <stdio.h>
#include <stdlib.h>
Line 110 ⟶ 464:
 
}
</syntaxhighlight>
</lang>
 
=={{header|C sharp|C#}}==
{{trans|JavaScript}}
<syntaxhighlight lang="csharp">using System;
 
class Program
{
public static bool JortSort<T>(T[] array) where T : IComparable, IEquatable<T>
{
// sort the array
T[] originalArray = (T[]) array.Clone();
Array.Sort(array);
 
// compare to see if it was originally sorted
for (var i = 0; i < originalArray.Length; i++)
{
if (!Equals(originalArray[i], array[i]))
{
return false;
}
}
 
return true;
}
}</syntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
#include <algorithm>
#include <string>
Line 176 ⟶ 555:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 185 ⟶ 564:
2 1 3 4 5 : -> The array is not sorted!
</pre>
 
=={{header|C sharp|C#}}==
{{trans|JavaScript}}
<lang csharp>using System;
 
class Program
{
public static bool JortSort<T>(T[] array) where T : IComparable, IEquatable<T>
{
// sort the array
T[] originalArray = (T[]) array.Clone();
Array.Sort(array);
 
// compare to see if it was originally sorted
for (var i = 0; i < originalArray.Length; i++)
{
if (!Equals(originalArray[i], array[i]))
{
return false;
}
}
 
return true;
}
}</lang>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(defn jort-sort [x] (= x (sort x)))</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">
{{incorrect|sort is destructive}}
(defun jort-sort (x)
<lang lisp>
(defun jort-sort (x) (equalp x (sort (copy-seq x) #'< )))
</syntaxhighlight>
</lang>
 
=={{header|Crystal}}==
<syntaxhighlight lang="ruby">def jort_sort(array)
array == array.sort
end</syntaxhighlight>
 
=={{header|D}}==
<syntaxhighlight lang="d">
<lang d>
module jortsort;
 
Line 238 ⟶ 597:
assert(!jortSort(["two", "one", "three"]));
}
</syntaxhighlight>
</lang>
 
=={{header|Delphi}}==
{{Trans|JavaScript}}
<syntaxhighlight lang="delphi">
program JortSort;
 
{$APPTYPE CONSOLE}
{$R *.res}
 
uses
System.SysUtils,
System.Generics.Collections,
System.Generics.Defaults;
 
type
TArrayHelper = class helper for TArray
public
class function JortSort<T>(const original: TArray<T>): Boolean; static;
end;
 
{ TArrayHelper }
 
class function TArrayHelper.JortSort<T>(const original: TArray<T>): Boolean;
var
sorted: TArray<T>;
i: Integer;
begin
SetLength(sorted, Length(original));
copy<T>(original, sorted, Length(original));
Sort<T>(sorted);
 
for i := 0 to High(original) do
if TComparer<T>.Default.Compare(sorted[i], original[i]) <> 0 then
exit(False);
Result := True;
end;
 
var
test: TArray<Integer>;
begin
// true
test := [1, 2, 3, 4, 5];
Writeln(TArray.JortSort<Integer>(test));
 
// false
test := [5, 4, 3, 2, 1];
Writeln(TArray.JortSort<Integer>(test));
 
Readln;
end.
</syntaxhighlight>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
proc sort . d[] .
for i = 1 to len d[] - 1
for j = i + 1 to len d[]
if d[j] < d[i]
swap d[j] d[i]
.
.
.
.
func jortsort arr[] .
orig[] = arr[]
sort arr[]
return if orig[] = arr[]
.
print jortsort [ 2 4 6 ]
print jortsort [ 4 2 6 ]
</syntaxhighlight>
{{out}}
<pre>
1
0
</pre>
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">iex(1)> jortsort = fn list -> list == Enum.sort(list) end
#Function<6.90072148/1 in :erl_eval.expr/5>
iex(2)> jortsort.([1,2,3,4])
true
iex(3)> jortsort.([1,2,5,4])
false</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
let jortSort n=n=Array.sort n
printfn "%A %A" (jortSort [|1;23;42|]) (jortSort [|23;42;1|])
</syntaxhighlight>
{{out}}
<pre>
true false
</pre>
=={{header|Factor}}==
<syntaxhighlight lang="factor">USING: kernel sorting ;
: jortsort ( seq -- ? ) dup natural-sort = ;</syntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
' Although it's possible to create generic sorting routines using macros in FreeBASIC
Line 310 ⟶ 759:
Print
Print "Press any key to quit"
Sleep</langsyntaxhighlight>
 
{{out}}
Line 320 ⟶ 769:
 
=={{header|Go}}==
<syntaxhighlight lang="go">
<lang go>
package main
 
Line 349 ⟶ 798:
}
 
</syntaxhighlight>
</lang>
 
=={{header|Haskell}}==
For lists:
<langsyntaxhighlight lang="haskell">import Data.List (sort)
 
jortSort :: (Ord a) => [a] -> Bool
jortSort list = list == sort list</langsyntaxhighlight>
 
or in applicative terms:
 
<syntaxhighlight lang="haskell">import Data.List (sort)
 
jortSort
:: (Ord a)
=> [a] -> Bool
jortSort = (==) <*> sort
 
--------------------------- TEST ---------------------------
main :: IO ()
main = print $ jortSort <$> [[4, 5, 1, 3, 2], [1, 2, 3, 4, 5]]</syntaxhighlight>
{{Out}}
<pre>[False,True]</pre>
 
=={{header|J}}==
Line 362 ⟶ 826:
 
'''Solution'''
<langsyntaxhighlight Jlang="j"> jortSortjortsort=: -: /:~</langsyntaxhighlight>
 
More in line with the spirit of the task would be:
 
<langsyntaxhighlight Jlang="j"> jortSort=: assert@(-: /:~)</langsyntaxhighlight>
 
Of course, ideally, assert would be replaced with something more assertive. Perhaps deleting all storage? But even better would be to send email to your boss and leadership explaining (at great length) exactly why they are all idiots. Do enough of this and you will never have to sort again...
 
'''Example Usage'''
<langsyntaxhighlight Jlang="j"> jortSortjortsort 1 2 4 3
0
jortSortjortsort 'sux'
1
jortSortjortsort&> 1 2 4 3;14 6 8;1 3 8 19;'ac';'sux';'CVGH';'PQRST'
0 0 1 1 1 0 1</langsyntaxhighlight>
 
Using jortSort in place of jortsort would throw an error on all but the first of those examples. It would return an empty result for that first example. (We have no way of representing the consequences of the more inane proposals presented here.)
 
=={{header|Janet}}==
<syntaxhighlight lang="janet">(defn jortsort [xs]
(deep= xs (sorted xs)))
 
(print (jortsort @[1 2 3 4 5])) # true
(print (jortsort @[2 1 3 4 5])) # false</syntaxhighlight>
 
=={{header|Java}}==
Optimized version of JortSort. Even less funny. Doesn't bother with sorting, but simply returns ''true''. Very fast. Use only when you're ''absolutely sure'' that the input is already sorted. You may have to use an unoptimized version of JortSort to ascertain this.
<langsyntaxhighlight lang="java">public class JortSort {
public static void main(String[] args) {
System.out.println(jortSort(new int[]{1, 2, 3}));
Line 388 ⟶ 861:
return true;
}
}</langsyntaxhighlight>
 
<pre>true</pre>
Line 395 ⟶ 868:
 
The original JavaScript implementation courtesy of the author, [https://github.com/jennschiffer/jortsort Jenn "Moneydollars" Schiffer].
<langsyntaxhighlight lang="javascript">var jortSort = function( array ) {
 
// sort the array
Line 407 ⟶ 880:
 
return true;
};</langsyntaxhighlight>
 
=={{header|jq}}==
<langsyntaxhighlight lang="jq">def jortsort: . == sort;</langsyntaxhighlight>
'''Example''':
<langsyntaxhighlight lang="jq">[1, "snort", "sort", [1,2], {"1":2}] | jortsort</langsyntaxhighlight>
{{Out}}
<syntaxhighlight lang ="sh">true</langsyntaxhighlight>
 
=={{header|Jsish}}==
Based on the Javascript satire.
<syntaxhighlight lang="javascript">/* jortSort in Jsish, based on the original satire, modified for jsish */
var jortSort = function(arr:array):boolean {
// make a copy
var originalArray = arr.slice(0);
// sort
arr.sort( function(a,b) { return a - b; } );
// compare to see if it was originally sorted
for (var i = 0; i < originalArray.length; ++i) {
if (originalArray[i] !== arr[i]) return false;
}
// yes, the data came in sorted
return true;
};
 
if (Interp.conf('unitTest')) {
; jortSort([1,2,3]);
; jortSort([3,2,1]);
; jortSort([1, 'snort', 'sort', [1,2], {1:2}]);
; jortSort(['snort', 'sort', 1, [1,2], {1:2}]);
}
 
/*
=!EXPECTSTART!=
jortSort([1,2,3]) ==> true
jortSort([3,2,1]) ==> false
jortSort([1, 'snort', 'sort', [1,2], {1:2}]) ==> true
jortSort(['snort', 'sort', 1, [1,2], {1:2}]) ==> false
=!EXPECTEND!=
*/</syntaxhighlight>
 
{{out}}
<pre>prompt$ jsish -u jortSort.jsi
[PASS] jortSort.jsi</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">
jortsort(A) = sort(A) == A
</syntaxhighlight>
</lang>
 
{{out}}
Line 438 ⟶ 947:
 
=={{header|K}}==
<syntaxhighlight lang="text">jortsort:{x~x@<x}</langsyntaxhighlight>
'''Example''':
<syntaxhighlight lang="text">jortsort 1 2 3</langsyntaxhighlight>
<pre>1
</pre>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
fun <T> jortSort(a: Array<T>): Boolean {
Line 469 ⟶ 978:
val d = arrayOf('C', 'D', 'A', 'E', 'B')
printResults(d)
}</langsyntaxhighlight>
 
{{out}}
Line 481 ⟶ 990:
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">function copy (t)
local new = {}
for k, v in pairs(t) do new[k] = v end
Line 494 ⟶ 1,003:
end
return true
end</langsyntaxhighlight>
 
=={{header|MathematicaMaple}}==
<syntaxhighlight lang Mathematica="maple">jortSort[list_] := list == Sort[list];proc(arr)
local copy:
copy := sort(Array([seq(arr[i], i=1..numelems(arr))])):
return ArrayTools:-IsEqual(copy,arr):
end proc:
#Examples
jortSort(Array([5,6,7,2,1]));
jortSort(Array([-5,0,7,12,21]));
jortSort(Array(StringTools:-Explode("abcdefg")));</syntaxhighlight>
{{Out|Output}}
<pre>false
true
true</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">jortSort[list_] := list == Sort[list];
Print[jortSort[Range[100]]];
Print[jortSort[RandomSample[Range[100]]]];</langsyntaxhighlight>
{{out}}
<pre>True
False</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import algorithm
 
func jortSort[T](a: openArray[T]): bool =
a == a.sorted()
 
proc test[T](a: openArray[T]) =
echo a, " is ", if a.jortSort(): "" else: "not ", "sorted"
 
test([1, 2, 3])
test([2, 3, 1])
echo ""
test(['a', 'b', 'c'])
test(['c', 'a', 'b'])</syntaxhighlight>
 
{{out}}
<pre>[1, 2, 3] is sorted
[2, 3, 1] is not sorted
 
['a', 'b', 'c'] is sorted
['c', 'a', 'b'] is not sorted</pre>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">function : JortSort(elems : CompareVector) ~ Bool {
sorted := CompareVector->New(elems);
sorted->Sort();
Line 516 ⟶ 1,062:
 
return true;
}</langsyntaxhighlight>
 
=={{header|OCaml}}==
For lists:
<langsyntaxhighlight lang="ocaml">let jortSortList lst =
lst = List.sort compare lst</langsyntaxhighlight>
For arrays:
<langsyntaxhighlight lang="ocaml">let jortSortArray ary =
let originalArray = Array.copy ary in
Array.sort compare ary;
originalArray = ary</langsyntaxhighlight>
 
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">: jortSort dup sort == ;</langsyntaxhighlight>
 
{{out}}
Line 540 ⟶ 1,086:
=={{header|ooRexx}}==
{{trans|REXX}}
<langsyntaxhighlight lang="oorexx">jortSort: Parse Arg list
/*---------------------------------------------------------------------
* Determine if list is sorted
Line 551 ⟶ 1,097:
End
Return (i=words(list)+1)|(list='')
</syntaxhighlight>
</lang>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">jortSort(v)=vecsort(v)==v</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">sub jortsort {
my @s=sort @_; # Default standard string comparison
for (0..$#s) {
Line 563 ⟶ 1,109:
}
1;
}</langsyntaxhighlight>
The task wants us to sort, but we could implement this by just using <tt>cmp</tt> on the input array elements, which would be faster (especially with unsorted input).
 
=={{header|Perl 6}}==
<lang perl6>sub jort-sort { @_ eqv @_.sort }</lang>
Actually, there's a better internal sort that seems to work best for lists that are already completely sorted, but tends to fails for any other list. The name of this sort, <tt>[!after]</tt>, is completely opaque, so we're pretty much forced to hide it inside a subroutine to prevent widespread panic.
<lang perl6>sub jort-sort-more-better-sorta { [!after] @_ }</lang>
However, since Perl 6 has a really good inliner, there's really little point anyway in using the <tt>[!after]</tt> reduction operator directly, and <tt>jort-sort-more-better-sorta</tt> is really much more self-documenting, so please don't use the reduction operator if you can. For example:
{{out}}
<pre>$ perl6
> [!after] <a b c> # DON'T do it this way
True
> [!after] 1,3,2 # DON'T do it this way either
False</pre>
Please do your part to uphold and/or downhold our community standards.
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>type JortSort(sequence s)
<span style="color: #008080;">type</span> <span style="color: #000000;">JortSort</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
return s=sort(s)
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
end type</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">type</span>
<!--</syntaxhighlight>-->
Then any variable or constant delared as type JortSort raises an error if used incorrectly, eg
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>JortSort ok = {1,2,3}
<span style="color: #000000;">JortSort</span> <span style="color: #000000;">ok</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}</span>
JortSort bad = {5,4,6}</lang>
<span style="color: #000000;">JortSort</span> <span style="color: #000000;">bad</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">}</span>
<!--</syntaxhighlight>-->
{{out}}
Note that while the above is fine under pwa/p2js and can be invoked explicitly, it does not automatically trigger the error.
<pre>
C:\Program Files (x86)\Phix\test.exw:2
Line 591 ⟶ 1,130:
</pre>
Amusingly the compiler itself uses a variant of jortsort, in that pttree.e declares a whole bunch of ternary tree node constants for all the language keywords and builtins such as
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>global constant T_while = 336 tt_stringF("while",T_while)</lang>
<span style="color: #008080;">global</span> <span style="color: #008080;">constant</span> <span style="color: #000000;">T_while</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">336</span> <span style="color: #000000;">tt_stringF</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"while"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">T_while</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
and if you change that to 338 and try to recompile the compiler, you'll immediately get:
<pre>
while should be 336(not 338)
</pre>
It does that because at the lowest level a cmp imm is at least twice as fast as a cmp [mem], and the only other way it could know these constants at compile-time would be to (re)build a 5000-node ternary tree, though I will concede that any sane person would have written a program to write an include file rather than hacking these things by hand.<br>
There is a similar thing in pwa\src\p2js_keywords.e, though it automatically checks, prompts, and auto-rebuilds that for you.
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">go =>
List = [
[1,2,3,4,5],
[2,3,4,5,1],
[2],
"jortsort",
"jortsort".sort()
],
foreach(L in List)
printf("%w: ", L),
if not jortsort(L) then
print("not ")
end,
println("sorted")
end,
nl.
 
jortsort(X) => X == X.sort().</syntaxhighlight>
 
{{out}}
<pre>[1,2,3,4,5]: sorted
[2,3,4,5,1]: not sorted
[2]: sorted
jortsort: not sorted
joorrstt: sorted</pre>
 
=={{header|PicoLisp}}==
<syntaxhighlight lang="picolisp">
<lang PicoLisp>
(de jortSort (L) (= L (sort L)))
(jortSort (1 2 3))
</syntaxhighlight>
</lang>
{{out}}
T
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
function jortsort($a) { -not (Compare-Object $a ($a | sort) -SyncWindow 0)}
jortsort @(1,2,3)
jortsort @(2,3,1)
</syntaxhighlight>
</lang>
<b>Output:</b>
<pre>
Line 619 ⟶ 1,188:
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang="purebasic">Macro isSort(liste)
If OpenConsole()
Print("[ ") : ForEach liste : Print(liste+Space(1)) : Next : Print("] = ")
Line 638 ⟶ 1,207:
For i=1 To 10 : AddElement(l1()) : l1()=Chr(Random(90,65)) : Next
isSort(l1()) : SortList(l1(),#PB_Sort_Ascending) : isSort(l1())
Input()</langsyntaxhighlight>
{{out}}
<pre>[ A Z Q G B N E B G Y ] = False
Line 644 ⟶ 1,213:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">>>> def jortSortjortsort(myarraysequence):
return list(myarraysequence) == sorted(myarraysequence)
>>> for data in [(1,2,4,3), (14,6,8), ['a', 'c'], ['s', 'u', 'x'], 'CVGH', 'PQRST']:
print(f'jortSortjortsort(%r{repr(data)}) is %s' % (data, jortSort{jortsort(data))}')
jortSortjortsort((1, 2, 4, 3)) is False
jortSortjortsort((14, 6, 8)) is False
jortSortjortsort(['a', 'c']) is True
jortSortjortsort(['s', 'u', 'x']) is True
jortSortjortsort('CVGH') is False
jortSortjortsort('PQRST') is True
>>> </langsyntaxhighlight>
 
=={{header|Quackery}}==
{{trans|JavaScript}}
 
<lang Python>def jortSort(array):
===The Task===
 
# sort the array
<code>jortsortwith</code> returns <code>true</code> if the nest is sorted, sort order is specified after <code>jortsortwith</code>, so <code>jortsortwith ></code> returns <code>true</code> if a nest of numbers is sorted in ascending numerical order, and <code>jortsortwith $<</code> returns <code>true</code> if a nest of strings is sorted in descending lexicographical order.
originalArray = list(array)
 
array.sort()
<syntaxhighlight lang="quackery"> [ dup
' [ sortwith ]
# compare to see if it was originally sorted
]'[ nested join
for i in range(len(originalArray)):
do = ] is jortsortwith ( [ --> b )</syntaxhighlight>
if originalArray[i] != array[i]:
 
return False
===The Non-satirical version===
 
return True</lang>
Jortsort, as specified in this task, is a sensible function ("is this nest sorted?) done inefficiently. This is the efficient version, with a more sensible name.
 
<syntaxhighlight lang="quackery"> [ true swap
]'[ temp put
dup [] != if
[ behead swap witheach
[ tuck temp share do if
[ dip not conclude ] ] ]
drop
temp release ] is sortedwith ( [ --> b )</syntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight Racketlang="racket">#lang racket/base
(define (jort-sort l [<? <])
(equal? l (sort l <?)))</langsyntaxhighlight>
 
Racket's <tt>sort</tt> function is efficient in that it starts by
checking the input, so the above could be made more efficient with a
pointer equality test:
<langsyntaxhighlight Racketlang="racket">#lang racket/base
(define (jort-sort l [<? <])
(eq? l (sort l <?)))</langsyntaxhighlight>
 
And an explicit implementation that checks the order (note that Racket's
<tt>sort</tt> expects a “smaller-than” comparator):
<langsyntaxhighlight Racketlang="racket">#lang racket/base
(define (jort-sort l [<? <])
(or (null? l)
(for/and ([x (in-list l)] [y (in-list (cdr l))])
(not (&lt;? y x))))) ; same as (&lt;= x y) but using only &lt;?</langsyntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" line>sub jort-sort { @_ eqv @_.sort }</syntaxhighlight>
Actually, there's a better internal sort that seems to work best for lists that are already completely sorted, but tends to fails for any other list. The name of this sort, <tt>[!after]</tt>, is completely opaque, so we're pretty much forced to hide it inside a subroutine to prevent widespread panic.
<syntaxhighlight lang="raku" line>sub jort-sort-more-better-sorta { [!after] @_ }</syntaxhighlight>
However, since Perl 6 has a really good inliner, there's really little point anyway in using the <tt>[!after]</tt> reduction operator directly, and <tt>jort-sort-more-better-sorta</tt> is really much more self-documenting, so please don't use the reduction operator if you can. For example:
{{out}}
<pre>$ perl6
> [!after] <a b c> # DON'T do it this way
True
> [!after] 1,3,2 # DON'T do it this way either
False</pre>
Please do your part to uphold and/or downhold our community standards.
 
=={{header|REXX}}==
Line 695 ⟶ 1,288:
The array elements (items) may be any form of number that REXX supports, and/or they can be alphabetic characters.
===using sort===
<langsyntaxhighlight lang="rexx">/*REXX program verifies that an array is sorted using a jortSort algorithm. */
parse arg $ /*obtain the list of numbers from C.L. */
if $='' then $=1 2 4 3 /*Not specified? Then use the default.*/
Line 720 ⟶ 1,313:
if !.k\==@.k then return 0 /*the array isn't sorted. */
end /*k*/
return 1 /*the array is sorted. */</langsyntaxhighlight>
'''output''' &nbsp; when using the default input: &nbsp; <tt> 1 &nbsp; 2 &nbsp; 4 &nbsp;3 </tt>
<pre>
Line 742 ⟶ 1,335:
 
Nothing is mentioned how it does this, and it certainly doesn't say that it sorts the input to verify if it's in order.
<langsyntaxhighlight lang="rexx">/*REXX program verifies that an array is sorted using a jortSort algorithm. */
parse arg $ /*obtain the list of numbers from C.L. */
if $='' then $=1 2 4 3 /*Not specified? Then use the default.*/
Line 756 ⟶ 1,349:
p=_
end /*j*/
return 1 /*array is sorted.*/</langsyntaxhighlight>
'''output''' &nbsp; is the same as the 1<sup>st</sup> REXX version. <br><br>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
aList = [4,2,3,1]
see jortSort(aList) + nl
Line 771 ⟶ 1,364:
next
return true
</syntaxhighlight>
</lang>
 
=={{header|Ruby}}==
<langsyntaxhighlight Rubylang="ruby">def jort_sort(array)
array == array.sort
end</langsyntaxhighlight>
 
{{trans|JavaScript}}
<langsyntaxhighlight Rubylang="ruby">def jort_sort(array)
# sort the array
original_array = array.dup
Line 790 ⟶ 1,383:
true
end</langsyntaxhighlight>
 
=={{header|Rust}}==
{{trans|JavaScript}}
<langsyntaxhighlight lang="rust">use std::cmp::{Ord, Eq};
 
fn jort_sort<T: Ord + Eq + Clone>(array: Vec<T>) -> bool {
Line 809 ⟶ 1,402:
 
return true;
}</langsyntaxhighlight>
 
 
'''Using iterators:'''
<syntaxhighlight lang="rust">
fn jort_sort<T>(slice: &[T]) -> bool
where
T: Ord + PartialEq + Clone,
{
let mut sorted = slice.to_vec();
sorted.sort_unstable();
 
slice
.iter()
.zip(sorted.iter())
.all(|(orig, sorted)| orig == sorted)
}</syntaxhighlight>
 
 
'''Idiomatic:'''
<syntaxhighlight lang="rust">
fn jort_sort<T>(slice: &[T]) -> bool
where
T: Ord + PartialEq + Clone,
{
let mut sorted = slice.to_vec();
sorted.sort_unstable();
 
slice == sorted
}</syntaxhighlight>
 
=={{header|Scala}}==
As of Scala 2.13, the .deep method has been removed from Array.
<lang scala>
The Java deepEquals() method will work as a replacement.
def jortSort[K:Ordering]( a:Array[K] ) = a.sorted.deep == a.deep
 
</lang>
<syntaxhighlight lang="scala">import java.util.Objects.deepEquals
 
def jortSort[K:Ordering]( a:Array[K] ) = deepEquals(a.sorted, a)</syntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func jort_sort(array) { array == array.sort };</langsyntaxhighlight>
 
=={{header|SSEM}}==
Line 825 ⟶ 1,450:
 
There are a couple of limitations that make the program less useful than it would otherwise be. Firstly, it is essentially a single-shot application: if you want to test a second array, you will need to manually reset storage address 0 to 16411 and storage address 26 to 0. Secondly, the SSEM's modest storage capacity means that the largest array you can sort (or not sort) using this program consists of (the terminating zero, and) four integers. Subject to those provisos, however, the program should be found to meet the specification satisfactorily.
<langsyntaxhighlight lang="ssem">11011000000000100000000000000000 0. -27 to c
00000000000000110000000000000000 1. Test
11101000000000000000000000000000 2. 23 to CI
Line 849 ⟶ 1,474:
11111111111111111111111111111111 22. -1
00001000000000000000000000000000 23. 16
01001000000000000000000000000000 24. 18</langsyntaxhighlight>
 
=={{header|Swift}}==
<langsyntaxhighlight Swiftlang="swift">func jortSort<T:Comparable>(array: [T]) -> Bool {
return array == sorted(array)
}</langsyntaxhighlight>
 
{{trans|JavaScript}}
<langsyntaxhighlight Swiftlang="swift">func jortSort<T:Comparable>(inout array: [T]) -> Bool {
// sort the array
Line 869 ⟶ 1,494:
return true
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
 
<langsyntaxhighlight lang="tcl">
proc jortsort {args} {
set list [lindex $args end]
Line 880 ⟶ 1,505:
expr {[lsort {*}$options $list] eq $list}
}
</syntaxhighlight>
</lang>
This supports all of the options known to the native '''lsort''' command, making it quite natural to use. The commented line ensures it will do the right thing for any list, even if it has funny formatting because it's embedded in source:
<pre>
Line 892 ⟶ 1,517:
1
</pre>
 
=={{header|UNIX Shell}}==
{{works with|Bourne Again SHell}}
 
<syntaxhighlight lang="sh">
JortSort() {
cmp -s <(printf “%s\n” “$@“) <(printf “%s\n” “$@“ | sort)
}
 
JortSortVerbose() {
if JortSort “$@“; then
echo True
else
echo False
If
}
 
JortSortVerbose 1 2 3 4 5
JortSortVerbose 1 3 4 5 2
JortSortVerbose a b c
JortSortVerbose c a b
</syntaxhighlight>
{{Out}}
<pre>True
False
True
False</pre>
 
=={{header|VBScript}}==
<langsyntaxhighlight lang="vb">Function JortSort(s)
JortSort = True
arrPreSort = Split(s,",")
Line 918 ⟶ 1,570:
WScript.StdOut.Write JortSort("a,b,c")
WScript.StdOut.WriteLine
WScript.StdOut.Write JortSort("a,c,b")</langsyntaxhighlight>
{{out}}
<pre>True
Line 924 ⟶ 1,576:
True
False</pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">
fn main() {
println(jort_sort([1, 2, 1, 11, 213, 2, 4])) //false
println(jort_sort([0, 1, 0, 0, 0, 0])) //false
println(jort_sort([1, 2, 4, 11, 22, 22])) //true
println(jort_sort([0, 0, 0, 1, 2, 2])) //true
}
fn jort_sort(a []int) bool {
mut c := a.clone()
c.sort()
for k, v in c {
if v == a[k] {
continue
} else {
return false
}
}
return true
}</syntaxhighlight>
 
{{out}}
<pre>
false
false
true
true
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-sort}}
<syntaxhighlight lang="wren">import "./sort" for Sort
 
var jortSort = Fn.new { |a|
var b = Sort.merge(a)
for (i in 0...a.count) {
if (a[i] != b[i]) return false
}
return true
}
 
var tests = [ [1, 2, 3, 4, 5], [2, 1, 3, 4, 5] ]
for (test in tests) System.print("%(test) -> %(jortSort.call(test) ? "sorted" : "not sorted")")</syntaxhighlight>
 
{{out}}
<pre>
[1, 2, 3, 4, 5] -> sorted
[2, 1, 3, 4, 5] -> not sorted
</pre>
 
=={{header|XPL0}}==
{{trans|Wren}}
<syntaxhighlight lang "XPL0">include xpllib; \for Sort
 
func JortSort(A, N);
int A, N, B, I;
def SizeOfInt = 4;
[B:= Reserve(N*SizeOfInt);
for I:= 0 to N-1 do B(I):= A(I);
Sort(B, N);
for I:= 0 to N-1 do
if B(I) # A(I) then return false;
return true;
];
 
int Tests, Test, I;
def Size = 5;
[Tests:= [ [1, 2, 3, 4, 5], [2, 1, 3, 4, 5] ];
for Test:= 0 to 2-1 do
[ChOut(0, ^[);
for I:= 0 to Size-2 do
[IntOut(0, Tests(Test,I)); Text(0, ", ")];
IntOut(0, Tests(Test,I));
Text(0, "] -> ");
Text(0, if JortSort(Tests(Test), Size) then "sorted" else "not sorted");
CrLf(0);
];
]</syntaxhighlight>
{{out}}
<pre>
[1, 2, 3, 4, 5] -> sorted
[2, 1, 3, 4, 5] -> not sorted
</pre>
 
=={{header|zkl}}==
Two "solutions", a linear one and one that actually sorts.
<langsyntaxhighlight lang="zkl">fcn jort(list){ False!=list.reduce(fcn(a,b){ (a>b) and return(Void.Stop,False); b }) }</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">fcn jort(list){ list==list.copy().sort() }</langsyntaxhighlight>
{{out}}
<pre>
1,983

edits