Solved

# Sort Numbers

Posted on 2011-05-09
343 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

LVL 32

Expert Comment

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

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

This line

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

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

LVL 32

Accepted Solution

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

it works :)
Thanks
0

## Featured Post

### Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…