Lazarus
Programming => General => Topic started by: tinytim33 on February 16, 2013, 10:01:33 pm
-
Hi,
I've created a listbox and am trying to use a bubble sort to output the list of names in sorted order.
I've tried various methods on the listbox to get the 'names' entered... but not compiling.
procedure TForm1.btnSortClick(Sender: TObject);
var NoMoreSwaps:boolean;
Temp:string;
count:integer;
begin
Repeat
For count:= 1 to lstName.items.count - 1
DO
If lstName.items.Names[count] > lstName.items.names[count+1]
THen
Begin
NoMoreSwaps:=false;
Temp:=lstName.items.Names[count];
lstName.items.names[count]:=lstName.items.names[count+1];
lstName.items.names[count + 1]:=temp;
End;
Until NoMoreSwaps = true;
end;
end.
I'm guessing lstName.items.names is the wrong method to access a value in the listbox.
Any ideas?
thanks, tiny
-
Try simply
lstName.items[count]
-
Is there a reason you want a bubblesort?
To sort the ListBox, it is enough to write
ListBox1.Sorted:=true;
-
Hi THanks,
It still won't compile for me. Error of type '' Out of Bounds ". ?
-
count:integer;
begin
Repeat
For count:= 1 to lstName.items.count - 1
DO
If lstName.items.Names[count] > lstName.items.names[count+1]
Reason is here. Maximal index can be "items.count-1". And you try to read ...items[count+1] which is in fact items[items.count].
Also, choose another name for indexing variable, "i" is enough. When its name is "count", it is difficult to explain where is the problem. :)
-
then
for count := 1 to max no. of items in list box?
-
No. I mean:
var i: integer;
begin
Repeat
For i:= 1 to lstName.items.count - 1 do
....
-
Many arrays, such as listbox items start from 0, not 1. For example array which item count is 10, would go from 0..9.
-
Am still getting lots of compile errors...but will try and source bug. Thanks for that...that helps me and makes sense. Will b back soon probably....
thanks, tiny
-
procedure TForm1.btnSortClick(Sender: TObject);
var NoMoreSwaps:boolean;
Temp:string;
count:integer;
begin
Repeat
NoMoreSwaps:=true;
For count:= 0 to lstName.items.count - 2
DO
If lstName.items [count] > lstName.items [count+1]
THen
Begin
NoMoreSwaps:=false;
Temp:=lstName.items [count];
lstName.items [count]:=lstName.items [count+1];
lstName.items [count + 1]:=temp;
End;
Until NoMoreSwaps = true;
end;