JortSort: Difference between revisions

Content added Content deleted
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 248: Line 248:
}
}
</lang>
</lang>

=={{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|C++}}==
=={{header|C++}}==
Line 322: Line 347:
2 1 3 4 5 : -> The array is not sorted!
2 1 3 4 5 : -> The array is not sorted!
</pre>
</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}}==
=={{header|Clojure}}==
Line 691: Line 691:
true
true
true</pre>
true</pre>



=={{header|Mathematica}}==
=={{header|Mathematica}}==
Line 763: Line 762:
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).
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}}==
=={{header|Phix}}==
<lang Phix>type JortSort(sequence s)
<lang Phix>type JortSort(sequence s)
Line 872: Line 859:
(for/and ([x (in-list l)] [y (in-list (cdr 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;?</lang>
(not (&lt;? y x))))) ; same as (&lt;= x y) but using only &lt;?</lang>

=={{header|Raku}}==
(formerly 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|REXX}}==
=={{header|REXX}}==