Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

delphi 7 custom sort listbox

Posted on 2009-05-17
11
Medium Priority
?
2,388 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 46

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 1600 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 38

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 46

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 38

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 46

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 38

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 400 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

722 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