Solved

delphi 7 custom sort listbox

Posted on 2009-05-17
11
2,267 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Working with hours 3 74
how can i search if string exist in array ? 3 69
Convert MS Word document to a PDF file 9 115
Using MMsystem To change audio input 1 19
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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