Jump to content

JortSort: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 248:
}
</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++}}==
Line 322 ⟶ 347:
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}}==
Line 691:
true
true</pre>
 
 
=={{header|Mathematica}}==
Line 763 ⟶ 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).
 
=={{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}}==
<lang Phix>type JortSort(sequence s)
Line 872 ⟶ 859:
(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>
 
=={{header|Perl 6Raku}}==
(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}}==
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.