Solved

delphi 7 custom sort listbox

Posted on 2009-05-17
11
2,192 Views
Last Modified: 2013-11-23
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?
0
Comment
Question by:jamerslong
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 24407351
Do you mean something like this CustomSort example?
http://www.swissdelphicenter.ch/torry/showcode.php?id=1664
0
 
LVL 4

Author Comment

by:jamerslong
ID: 24407619
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
 
LVL 12

Accepted Solution

by:
Hypo earned 400 total points
ID: 24407994
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
 
LVL 12

Expert Comment

by:Hypo
ID: 24408026
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
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 24409299
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 45

Expert Comment

by:aikimark
ID: 24411372
@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
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 24411399
@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
 
LVL 45

Expert Comment

by:aikimark
ID: 24411478
@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
 
LVL 4

Author Comment

by:jamerslong
ID: 24471196
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
 
LVL 37

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 100 total points
ID: 24471636
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
 
LVL 4

Author Closing Comment

by:jamerslong
ID: 31582405
Thanks for the help guys
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
APK file name 7 64
Read/Write registry value from HKEY_LOCAL_MACHINE 5 179
Delphi - replicating a form 8 57
Delphi selector screen 2 58
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now