delphi 7 custom sort listbox

Ok I need to custom sort a listbox into alphabetical order. I need to use the Insert command to add the item, but how do I search the listbox and find the right place to insert the item? I need to know what the Index is of the item I add.

can anyone point me in the right direction as to how i would filter through the ListBox to find the correct spot to insert a new item?
LVL 4
jamerslongAsked:
Who is Participating?
 
HypoConnect With a Mentor Commented:
What you are looking for is something that is similar to the InsertSort algorithm, which is an algorithm that uses binary search to locate the index of where the item should go, and then inserts the item at that point. Check out the example below, it holds the test you want sorted in a Memo, and when you press the Button, it loops through all the texts, and inserts them to the ListBox into the correct order. The result is that the ListBox contains the lines from Memo1, but sorted. (I use case insensetive sorting, but you can change that to CaseSensetive by replacing CompareText with CompareStr in the BinSearch function.

regards
Hypo

regards
Hypo.
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.FormCreate(Sender: TObject);
var i, j : Integer;
    S : String;
begin
  // Create 50 random texts...
  for i := 0 to 50 do begin
    S := '';
    for j := 0 to random(10) do
      Case Random(3) of
       0: S := S + chr(ord('a')+Random(26));
       1: S := S + chr(ord('A')+Random(26));
       2: S := S + chr(ord('0')+Random(9));
      end;
    Memo1.Lines.Add(S);
  end;
end;
 
 
Function FindSortedIndex(AText : String; ALines : TStrings) : Integer;
  Function BinSearch(A, B : Integer) : Integer;
  var pivot : Integer;
  Begin
    If (A = B)
      then Result := A
      else begin
        pivot := (A + B) shr 1;
        if CompareText(AText, ALines[pivot]) <= 0
          then Result := BinSearch(A, Pivot)
          else Result := BinSearch(Pivot+1, B);
      end;
  End;
Begin
  Result := BinSearch(0, ALines.Count);
End;
 
procedure TForm1.Button1Click(Sender: TObject);
var i, j : Integer;
begin
  ListBox1.Clear;
  for i := 0 to Memo1.Lines.Count-1 do begin
    j := FindSortedIndex(Memo1.Lines[i], ListBox1.Items);
    if j = -1
      then ListBox1.Items.Add(Memo1.Lines[i])
      else ListBox1.Items.Insert(j, Memo1.Lines[i]);
  end;
end;
 
end.

Open in new window

0
 
aikimarkCommented:
Do you mean something like this CustomSort example?
http://www.swissdelphicenter.ch/torry/showcode.php?id=1664
0
 
jamerslongAuthor Commented:
no, I dont want to use custom sort, i would like to do this with a button click e.g.

for i := 0 to listbox1.items.count -1
begin
//compare edit1.text with item at index i, if the aphabetical sort says it goes at this spot then Listbox1.Items.insert(i, edit1.text);
end

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
HypoCommented:
Worth mentioning is that such an algorithm requires that the target list (ListBox1.Items in this case) is sorted or empty when then algorithm starts executing.

regards
Hypo.
0
 
Geert GOracle dbaCommented:
and why not just sorted := True and then use Add ?
the list automatically finds where to insert the line

no fancy code needed ... except from Hypo ...
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;
 
type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Button1: TButton;
    Panel1: TPanel;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.Button1Click(Sender: TObject);
var J: Integer; S: string;
begin
  for j := 0 to random(10) do
      Case Random(3) of
       0: S := S + chr(ord('a')+Random(26));
       1: S := S + chr(ord('A')+Random(26));
       2: S := S + chr(ord('0')+Random(9));
      end;
  Panel1.Caption := s;
  Listbox1.AddItem(S, nil);
end;
 
procedure TForm1.FormCreate(Sender: TObject);
var i, j : Integer;
    S : String;
begin
  // Create 50 random texts...
  for i := 0 to 50 do
  begin
    S := '';
    for j := 0 to random(10) do
      Case Random(3) of
       0: S := S + chr(ord('a')+Random(26));
       1: S := S + chr(ord('A')+Random(26));
       2: S := S + chr(ord('0')+Random(9));
      end;
    Listbox1.AddItem(S, nil);
  end;
end;

Open in new window

0
 
aikimarkCommented:
@geert

If the listbox contains items that require a special sorting routine, such as dates, then the listbox's alpha sort logic won't work.
0
 
Geert GOracle dbaCommented:
@aikimark
aye skipper, I know.

@author>>no, I dont want to use custom sort

basically using custom sort, setting sorted to true and the using add
is the same as
searching for the record using a index search, and inserting the record there.

first will always work, second is not full proof from all directions (like other programmers on same source)
0
 
aikimarkCommented:
@jamerslong

If your code doesn't have control of the population of the listbox, you might have to re-populate the listbox to ensure it conforms to your ordering scheme.
0
 
jamerslongAuthor Commented:
Ok. so i have had to step away for a bit, For Geert, I want to use the method, but how do I know what the ItemIndex of where the item is being inserted when the listbox is being sorted.

I dont even remember Exactly what my plan was, but Hypo has it in the spot! it lets me know what the index Is when the Item is inserted without having a bunch of trickery. i will wait for geert to respond before a decision is made though. i dont want to prevent him from getting assist points if it is deserved.

aikimark, the Link Posted tell me how to do a proper sort, but how do i know what the ItemIndex is?

I just figgured if i did the sort outside of the listbox then I can find out the Item Index without trouble.
I am a C++ guy and using the listbox built in Sorting thing looks like it is missing a part....so i am kinda lost as to where it is getting the ItemIndex from :(
0
 
Geert GConnect With a Mentor Oracle dbaCommented:
jamerslong.
a listbox has a property called sorted = true/false
in essence this calls the sort routine.
This sort routine calls customsort with a Quicksort function var and this uses a function CompareStrings

function TStrings.CompareStrings(const S1, S2: string): Integer; override;
function TStringList.CompareStrings(const S1, S2: string): Integer; override;

if Delphi Dev would have done it good they would have made a procedural variable for the sorting mechanism, but off course, they didn't !

For your own ordering of the strings you would override this method and that would have been it.
The problem is that the items from a Listbox are of type TListboxString and this is defined in the implementation of the unit StdCtrls. So that is the main problem.
Neither can we create a descendant with our own sorting routine ! dugh ! talk of a cumbersome problem

so i actually will have to rest my case ...
it seems the best way at the moment is to indeed find the itemindex
and insert at that position.

they didn't really leave a lot to customize.
the fItems are private declared and created, so can't change them to a new type
if they would have created a function to create the FItems that would have solved it !
but alas no, this is one of those holes they haven't filled yet !
0
 
jamerslongAuthor Commented:
Thanks for the help guys
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.