Sort using a custom comparator: Difference between revisions
Content added Content deleted
No edit summary |
(→{{header|Pascal}}: added example) |
||
Line 3,284: | Line 3,284: | ||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
{{works with|Free Pascal}} |
{{works with|Free Pascal}} |
||
<syntaxhighlight lang="pascal"> |
|||
See at http://rosettacode.org/wiki/Sorting_algorithms/Merge_sort#improvement struct/record with myText implementing this task too |
|||
program CustomComparator; |
|||
{$mode objfpc}{$h+} |
|||
uses |
|||
Classes, SysUtils, Math; |
|||
function Compare(List: TStringList; Index1, Index2: Integer): Integer; |
|||
begin |
|||
Result := CompareValue(Length(List[Index2]), Length(List[Index1])); |
|||
if Result = 0 then |
|||
Result := CompareText(List[Index1], List[Index2]); |
|||
end; |
|||
const |
|||
Sample = 'Here are some sample strings to be sorted'; |
|||
begin |
|||
with TStringList.Create do |
|||
try |
|||
AddStrings(Sample.Split([' '], TStringSplitOptions.ExcludeEmpty)); |
|||
CustomSort(@Compare); |
|||
WriteLn(string.Join(', ', ToStringArray)); |
|||
finally |
|||
Free; |
|||
end; |
|||
Readln; |
|||
end. |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
strings, sample, sorted, Here, some, are, be, to |
|||
</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |