Solved

# Sorting numbers

Posted on 2012-09-15
528 Views
I have a listbox containing rows of numbers: 7 numbers per row separated by commas, such as:

27,11,44,23,3,9,7
15,6,27,22,44,2,4
15,35,11,27,42,4,6
etc

When I click a button, I need each of these numbers to be added to another listbox, one per line, in acending order (sorted):

2
4
4
6
7
9
11
11
etc

I am using Delphi 6.  Many thanks for your help.
0
Question by:rincewind666

LVL 22

Accepted Solution

``````function CompareInt(List: TStringList; Index1, Index2: Integer): Integer;
var
d1, d2: Integer;
r1, r2: Boolean;
function IsInt(AString: string; var AInteger: Integer): Boolean;
var
Code: Integer;
begin
Val(AString, AInteger, Code);
Result := (Code = 0);
end;

begin
r1 := IsInt(List[Index1], d1);
r2 := IsInt(List[Index2], d2);
Result := ord(r1 or r2);
if Result <> 0 then
begin
if d1 < d2 then
Result := -1
else if d1 > d2 then
Result := 1
else
Result := 0;
end
else
Result := lstrcmp(PChar(List[Index1]), PChar(List[Index2]));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
l: TStringList;
i: Integer;
begin
l := TStringList.Create;
try
ListBox2.Items.BeginUpdate;
ListBox2.Clear;
for i := 0 to ListBox1.Items.Count-1 do
begin
l.Delimiter := ',';
l.DelimitedText := ListBox1.Items[i];
end;
finally
l.Assign(ListBox2.Items);
l.CustomSort(CompareInt);
ListBox2.Items.Assign(l);
ListBox2.Items.EndUpdate;
l.free;
end;
end;
``````
0

LVL 9

Expert Comment

Looks like homework.
0

Author Closing Comment

Many thanks Ferruccio68. Your help is greatly appreciated.
0

## Featured Post

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 I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

#### Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!