?
Solved

How to sort items of ListBox ?

Posted on 1998-09-21
11
Medium Priority
?
299 Views
Last Modified: 2010-04-06
Is it a way to sort the items of a ListBox
starting the caracter n°6 ?
Example:
no sorted :      --->          sorted

22005Armageddon          22007Akkenaton
22006Babouchka   --->    22005Armageddon
22007Akkenaton           22006Babouchka
22008Godzilla            22008Godzilla

Thanks for your help.
0
Comment
Question by:nk51
[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
11 Comments
 
LVL 7

Expert Comment

by:BlackMan
ID: 1340424
You will have to set the Sorted property to False and then sort the strings prior to adding them to the ListBox.
0
 
LVL 4

Accepted Solution

by:
erajoj earned 30 total points
ID: 1340425
Hi,
Here's a way of sorting the way you want (postsort):

type
  TStringsSortCompare = function (List: TStrings; Item1, Item2: Integer): Integer;

function SortTrail6( List: TStrings; Item1, Item2: Integer): Integer;
var
  s1, s2: string;
begin
  s1 := List[ Item1 ]; Delete( s1, 1, 5 ); s2 := List[ Item2 ]; Delete( s2, 1, 5 );
  Result := CompareText( s1, s2 );
end;

function SortStrings( List: TStrings; SortProc: TStringsSortCompare ): Boolean;
  procedure QuickSort(L, R: Integer);
  var
    I, J, P: Integer;
  begin
    repeat
      I := L;
      J := R;
      P := (L + R) shr 1;
      repeat
        while SortProc(List, I, P) < 0 do Inc(I);
        while SortProc(List, J, P) > 0 do Dec(J);
        if I <= J then
        begin
          List.Exchange(I, J);
          Inc(I);
          Dec(J);
        end;
      until I > J;
      if L < J then QuickSort(L, J);
      L := I;
    until I >= R;
  end;
begin
  if Assigned( List ) then
  begin
    Result := True;
    QuickSort( 0, List.Count - 1 );
  end else Result := False;
end;

Here's how to use it:

procedure TForm1.Button1Click(Sender: TObject);
begin
  SortStrings( ListBox1.Items, SortTrail6 );
end;

/// John
0
 
LVL 4

Expert Comment

by:binkzz
ID: 1340426
What you could do is make an invisible little listbox,
take the numbers of the original items and put the in the
other listbox, then copy the listbox2 to the listbox
and making sure the original listbox hasn't got the sorted
property set to true.

Hope this helps,
Tom
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Expert Comment

by:inter
ID: 1340427
Hi,
The best way -at least mine- is to store that kind of values in Objects property of the TListBox's Lines property. Example,
Say you want to add the things above to a ListBox(or TStringList)

var
  Idx : integer;
begin
  ListBox1.Items.Clear;
  ListBox1.Items.Sorted := true;
  ListBox1.Lines.Add('Armageddon',Pointer(22005));
  ListBox1.Lines.Add('Babouchka',Pointer(22006));
  ListBox1.Lines.Add('Akkenaton',Pointer(22007));
  ListBox1.Lines.Add('Godzilla',Pointer(22008));
  //now assume you want to find what is the number associated with Godzilla
  if ListBox1.Lines.Find('Godzilla', idx) then
  begin
     ShowMessage(ListBox1.Lines[idx]+' has value' +
         IntToStr(Integer(ListBox1.Lines.Objects[idx])));
  end;
end;
regards, igor
0
 

Author Comment

by:nk51
ID: 1340428
I think erajoj has proposed the best nearest solution for my problem.
Thinks to you ERAJOJ !

0
 

Author Comment

by:nk51
ID: 1340429
I want to say :
thAnks to you ERAJOJ !
of course

And thanks to others for your solutions.
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1340430
Hi,
What do you mean "nearest"?
If you do not think it's a complete solution, don't accept it.
I do not like providing semicrappy answers.
What else do you want?
If you wanted a TListBox decendant with automatic sorting
to replace the original one, you could just ask, you know.
That's what we're here for. :-)

/// John
0
 

Author Comment

by:nk51
ID: 1340431
I want to say :
thAnks to you ERAJOJ !
of course

And thanks to others for your solutions.
0
 

Author Comment

by:nk51
ID: 1340432
Don't panic Erajoj !
Your solution is the solution I waited !
Nearest was regarding the other answers.
If a solution is bad or doesn't correspond to what I'm waiting, I refuse it !

NK51 (-;
0
 
LVL 4

Expert Comment

by:erajoj
ID: 1340433
Good!
Just what I wanted to know.
Can't keep track of the qualifications of all of you, you know.
No panic, just don't like to rob the points of people when my
answers are mediocre.

/// John
0
 

Author Comment

by:nk51
ID: 1340434
Ok, no problem.
And I have only a few point to ask questions,
so I'm prudent with the reward.

   |
(-;|]
   |
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

719 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