This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -3,26 +3,27 @@
#include <cctype>
// compare character case-insensitive
bool icompare_char(char c1, char c2)
{
return std::toupper(c1) < std::toupper(c2);
}
struct icompare_char {
bool operator()(char c1, char c2) {
return std::toupper(c1) < std::toupper(c2);
}
};
// return true if s1 comes before s2
bool compare(std::string const& s1, std::string const& s2)
{
if (s1.length() > s2.length())
return true;
if (s1.length() < s2.length())
return false;
return std::lexicographical_compare(s1.begin(), s1.end(),
s2.begin(), s2.end(),
icompare_char);
}
struct compare {
bool operator()(std::string const& s1, std::string const& s2) {
if (s1.length() > s2.length())
return true;
if (s1.length() < s2.length())
return false;
return std::lexicographical_compare(s1.begin(), s1.end(),
s2.begin(), s2.end(),
icompare_char());
}
};
int main()
{
int main() {
std::string strings[8] = {"Here", "are", "some", "sample", "strings", "to", "be", "sorted"};
std::sort(strings, strings+8, compare);
std::sort(strings, strings+8, compare());
return 0;
}

View file

@ -0,0 +1,8 @@
import std.stdio, std.string, std.algorithm, std.typecons;
void main() {
"here are Some sample strings to be sorted"
.split
.schwartzSort!q{ tuple(-a.length, a.toUpper) }
.writeln;
}

View file

@ -0,0 +1,9 @@
void main() {
import std.stdio, std.string, std.algorithm;
auto parts = "here are Some sample strings to be sorted".split;
parts.multiSort!("a.length > b.length", "a.toUpper < b.toUpper");
parts.writeln;
}

View file

@ -0,0 +1,12 @@
-module( sort_using_custom_comparator ).
-export( [task/0] ).
task() ->
lists:sort( fun longest_first_case_insensitive/2, ["this", "is", "a", "set", "of", "strings", "to", "sort", "This", "Is", "A", "Set", "Of", "Strings", "To", "Sort"] ).
longest_first_case_insensitive( String1, String2 ) when erlang:length(String1) =:= erlang:length(String2) -> string:to_lower(String1) < string:to_lower(String2);
longest_first_case_insensitive( String1, String2 ) when erlang:length(String1) =< erlang:length(String2) -> false;
longest_first_case_insensitive( _String1, _String2 ) -> true.

View file

@ -0,0 +1,13 @@
using System.Console;
module CustomSort
{
Main() : void
{
def strings1 = ["these", "are", "strings", "of", "different", "length"];
def strings2 = ["apple", "House", "chewy", "Salty", "rises", "Later"];
WriteLine(strings1.Sort((x, y) => y.Length.CompareTo(x.Length)));
WriteLine(strings2.Sort((x, y) => x.CompareTo(y)))
}
}

View file

@ -0,0 +1,2 @@
v = c("Here", "are", "some", "sample", "strings", "to", "be", "sorted")
print(v[order(-nchar(v), tolower(v))])

View file

@ -0,0 +1,130 @@
/*REXX program sorts a (stemmed) array using the merge-sort method. */
/* using mycmp function for the sort order */
/**********************************************************************
* mergesort taken from REXX (adapted for ooRexx (and all other REXXes))
* 28.07.2013 Walter Pachl
**********************************************************************/
Call gena /* generate the array elements. */
Call showa 'before sort' /* show the before array elements.*/
Call mergeSort highitem /* invoke the merge sort for array*/
Call showa ' after sort' /* show the after array elements.*/
Exit /* stick a fork in it, we're done.*/
/*---------------------------------GENa subroutine-------------------*/
gena:
a.='' /* assign default value for a stem*/
a.1='---The seven deadly sins---'/* everybody: pick your favorite.*/
a.2='==========================='
a.3='pride'
a.4='avarice'
a.5='wrath'
a.6='envy'
a.7='gluttony'
a.8='sloth'
a.9='lust'
Do highitem=1 While a.highitem\=='' /*find number of entries */
End
highitem=highitem-1 /* adjust highitem by -1. */
Return
/*---------------------------------MERGETOa subroutine---------------*/
mergetoa: Procedure Expose a. !.
Parse Arg l,n
Select
When n==1 Then
Nop
When n==2 Then Do
h=l+1
If mycmp(a.l,a.h)=1 Then Do
_=a.h
a.h=a.l
a.l=_
End
End
Otherwise Do
m=n%2
Call mergeToa l+m,n-m
Call mergeTo! l,m,1
i=1
j=l+m
Do k=l While k<j
If j==l+n|mycmp(!.i,a.j)<>1 Then Do
a.k=!.i
i=i+1
End
Else Do
a.k=a.j
j=j+1
End
End
End
End
Return
/*---------------------------------MERGESORT subroutine--------------*/
mergesort: Procedure Expose a.
Call mergeToa 1,arg(1)
Return
/*---------------------------------MERGETO! subroutine---------------*/
mergeto!: Procedure Expose a. !.
Parse Arg l,n,_
Select
When n==1 Then
!._=a.l
When n==2 Then Do
h=l+1
q=1+_
If mycmp(a.l,a.h)=1 Then Do
q=_
_=q+1
End
!._=a.l
!.q=a.h
Return
End
Otherwise Do
m=n%2
Call mergeToa l,m
Call mergeTo! l+m,n-m,m+_
i=l
j=m+_
Do k=_ While k<j
If j==n+_|mycmp(a.i,!.j)<>1 Then Do
!.k=a.i
i=i+1
End
Else Do
!.k=!.j
j=j+1
End
End
End
End
Return
/*---------------------------------SHOWa subroutine------------------*/
showa:
widthh=length(highitem) /* maximum the width of any line.*/
Do j=1 For highitem
Say 'element' right(j,widthh) arg(1)':' a.j
End
Say copies('-',60) /* show a separator line (fence).*/
Return
mycmp: Procedure
/**********************************************************************
* shorter string considered higher
* when lengths are equal: caseless 'Z' considered higher than 'X' etc.
* Result: 1 B consider higher than A
* -1 A consider higher than B
* 0 A==B (caseless)
**********************************************************************/
Parse Upper Arg A,B
A=strip(A)
B=strip(B)
I = length(A)
J = length(B)
Select
When I << J THEN res=1
When I >> J THEN res=-1
When A >> B THEN res=1
When A << B THEN res=-1
Otherwise res=0
End
RETURN res