Solved

Sort Numbers

Posted on 2011-05-09
Medium Priority
347 Views
I have a string that contains numbers like
88 -77 90 4 8 1 8

I need a function that take this string and sort the numbers and return it in order like this
90 88 8 8 4 1 -77

the code should be like this

``````var
S,x : String;
begin
S := '88 -77 90 4 8 1 8';

X := Sort(S);
End;

Function Sort(S:String):String;
Begin
///
End;
``````

Can any help with this function?
0
Question by:MissManal
• 3
• 2

LVL 32

Expert Comment

ID: 35724844
Try this
``````function DescendingSort(List: TStringList; Index1, Index2: Integer): Integer;
begin
Result := StrToInt(Trim(List[Index2])) - StrToInt(Trim(List[Index1]));
end;

function Sort(const S: string): string;
var
List: TStringList;
begin
List := TStringList.Create;
try
List.Delimiter := ',';
List.StrictDelimiter := True;
List.DelimitedText := StringReplace(S, ' ', ',', [rfReplaceAll]);
List.CustomSort(DescendingSort);
Result := List.Text;
finally
FreeAndNil(List);
end;
end;

procedure TForm3.Button1Click(Sender: TObject);
var
S: string;
begin
S := '88 -77 90 4 8 1 8';
S := Sort(S);
ShowMessage(S);
end;
``````
0

LVL 32

Expert Comment

ID: 35724881
Modify it to this
``````function DescendingSort(List: TStringList; Index1, Index2: Integer): Integer;
begin
Result := StrToInt(Trim(List[Index2])) - StrToInt(Trim(List[Index1]));
end;

function Sort(const S: string): string;
var
List: TStringList;
begin
List := TStringList.Create;
try
List.Delimiter := ',';
List.StrictDelimiter := True;
List.DelimitedText := StringReplace(S, ' ', ',', [rfReplaceAll]);
List.CustomSort(DescendingSort);
Result := StringReplace(StringReplace(List.Text, #10, ' ', [rfReplaceAll]), #13, ' ', [rfReplaceAll]);
finally
FreeAndNil(List);
end;
end;

procedure TForm3.Button1Click(Sender: TObject);
var
S: string;
begin
S := '88 -77 90 4 8 1 8';
S := Sort(S);
ShowMessage(S);
end;
``````
0

Author Comment

ID: 35724911
This line

``````List.CustomSort(DescendingSort);
``````

show this error message
E2009 Incompatible types: 'regular procedure and method pointer'
0

LVL 32

Accepted Solution

Ephraim Wangoya earned 2000 total points
ID: 35724924

Dont make DescendingSort a method of the form, it should only appear after implementation
``````unit Unit3;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm3 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
function Sort(const S: string): string;
end;

var
Form3: TForm3;

implementation

{\$R *.dfm}

function DescendingSort(List: TStringList; Index1, Index2: Integer): Integer;
begin
Result := StrToInt(Trim(List[Index2])) - StrToInt(Trim(List[Index1]));
end;

{ TForm3 }

function TForm3.Sort(const S: string): string;
var
List: TStringList;
begin
List := TStringList.Create;
try
List.Delimiter := ',';
List.StrictDelimiter := True;
List.DelimitedText := StringReplace(S, ' ', ',', [rfReplaceAll]);
List.CustomSort(DescendingSort);
Result := StringReplace(StringReplace(List.Text, #10, ' ', [rfReplaceAll]), #13, ' ', [rfReplaceAll]);
finally
FreeAndNil(List);
end;
end;

procedure TForm3.Button1Click(Sender: TObject);
begin
ShowMessage(Sort('88 -77 90 4 8 1 8'));
end;

end.
``````
0

Author Comment

ID: 35725040
it works :)
Thanks
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month15 days, 5 hours left to enroll