Link to home
Start Free TrialLog in
Avatar of nk51
nk51

asked on

How to sort items of ListBox ?

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.
Avatar of BlackMan
BlackMan

You will have to set the Sorted property to False and then sort the strings prior to adding them to the ListBox.
ASKER CERTIFIED SOLUTION
Avatar of erajoj
erajoj
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
Avatar of nk51

ASKER

I think erajoj has proposed the best nearest solution for my problem.
Thinks to you ERAJOJ !

Avatar of nk51

ASKER

I want to say :
thAnks to you ERAJOJ !
of course

And thanks to others for your solutions.
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
Avatar of nk51

ASKER

I want to say :
thAnks to you ERAJOJ !
of course

And thanks to others for your solutions.
Avatar of nk51

ASKER

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 (-;
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
Avatar of nk51

ASKER

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

   |
(-;|]
   |