• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 379
  • Last Modified:

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
0
xpher
Asked:
xpher
  • 2
1 Solution
 
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
 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now