FileListBox - Search - Sort

I am displaying filenames in FileListBox (obvious I suppose) my file names are peoples names ie MR A PERSON.jpg. I have no problem displaying without file extension but what I would like to do is have the list sorted alphabetically by surname (keeping name format same as above) and I would also like to search by surname and return item index.

I know this sounds like two questions in one but it is all tied together so I couldn't split.

Many thanks
Chris
LVL 1
xpherAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chrbCommented:
You could make your own sub class of the TFileListBox where you override ReadFilesNames and sort the Items list

TMyFileListBox = class(TFileListBox)
protected
  procedure ReadFileNames; override;
end;

procedure TMyFileListBox.ReadFilesName;
begin
  inherited;
  {Here you sort the list}
end;

And you could add your own method IndexOf or so to search on surname.

Christian
0
kubeerjaCommented:
if you don't want to go into making a sub class then you can use a simple sort procedure :

procedure TForm1.SortListBox ;
var
 I,J : Integer ;
 s : String ;
begin
  for I:= 0 to ListBox1.items.Count-2 do
  begin
      for J:= I to ListBox1.items.Count-1 do
      begin
        if AnsiCompareStr(getsurname(ListBox1.items[I]),getsurname(ListBox1.items[J]))>0 then
        begin
          S:= ListBox1.items[I] ;
          ListBox1.items[I]:=ListBox1.items[J] ;
          ListBox1.items[J] := S ;
        end;
      end ;
  end ;
end;

function TForm1.getsurname(s : String) : String ;
begin
  result := copy(s,pos(' ',s)+1,length(s)) ;
  result := copy(result,pos(' ',result)+1,length(result)) ;
end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
xpherAuthor Commented:
Your getsurname function I don't quite follow. Bearing in mind that before the surname it could be
Mrs A Surname
Ms A Surname
Mr A Surname
A Surname

Chris
0
kubeerjaCommented:
this function will work with all the cases you list and get the surname.
the sort will work on the surname only and not on the surname and first name in the same time
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.