program dp_178240;
{$APPTYPE CONSOLE}
{$R *.res}
{.$DEFINE USE_TYPES}
uses
{$IFDEF USE_TYPES}
Types,
// Gibt es ab Delphi XE ???
{$ENDIF}
Classes,
SysUtils;
{$IFNDEF USE_TYPES}
type
TIntegerDynArray =
array of Integer;
{$ENDIF}
function StrToIntArray(
const AStr :
string;
const ADelimiter : Char ) : TIntegerDynArray;
var
LStrings : TStrings;
LIdx : Integer;
begin
LStrings := TStringList.Create;
try
LStrings.Delimiter := ADelimiter;
LStrings.DelimitedText := AStr;
SetLength( Result, LStrings.Count );
for LIdx := 0
to LStrings.Count - 1
do
begin
Result[LIdx] := StrToInt( LStrings[LIdx] );
end;
finally
LStrings.Free;
end;
end;
function IntArrayToStr(
const AIntArray : TIntegerDynArray;
const ADelimiter : Char ) :
string;
var
LStrings : TStrings;
LIdx : Integer;
begin
LStrings := TStringList.Create;
try
LStrings.Delimiter := ADelimiter;
for LIdx := low( AIntArray )
to high( AIntArray )
do
begin
LStrings.Add( IntToStr( AIntArray[LIdx] ) );
end;
Result := LStrings.DelimitedText;
finally
LStrings.Free;
end;
end;
procedure SortIntArray(
var AIntArray : TIntegerDynArray );
var
LCurrent : Integer;
LCompare : Integer;
LTemp : Integer;
begin
// Simple Bubble-Sort algo
for LCurrent := low( AIntArray )
to high( AIntArray ) - 1
do
begin
for LCompare := LCurrent + 1
to high( AIntArray )
do
begin
if AIntArray[LCurrent] > AIntArray[LCompare]
then
begin
LTemp := AIntArray[LCurrent];
AIntArray[LCurrent] := AIntArray[LCompare];
AIntArray[LCompare] := LTemp;
end;
end;
end;
end;
function SortIntArrayStr(
const AStr :
string;
const ADelimiter : Char = '
,' ) :
string;
var
LIntArray : TIntegerDynArray;
begin
LIntArray := StrToIntArray( AStr, ADelimiter );
SortIntArray( LIntArray );
Result := IntArrayToStr( LIntArray, ADelimiter );
end;
procedure Test;
var
LTest :
string;
begin
LTest := '
7,3,11,5,1';
Writeln( LTest, '
=> ', SortIntArrayStr( LTest ) );
end;
begin
try
Test;
except
on E :
Exception do
Writeln( E.ClassName, '
: ', E.
Message );
end;
ReadLn;
end.