Recent

Author Topic: [Solved] How to sort the files in the File List Box?  (Read 443 times)

loaded

  • Hero Member
  • *****
  • Posts: 824
[Solved] How to sort the files in the File List Box?
« on: February 05, 2023, 04:23:46 pm »
Hi All,
I'm trying to view files in a specific folder in my respective application. But in the file list box it looks like sorting doesn't work. Attached is the correct view in the windows file explorer, and the wrong view in the file list box that belongs to me. Is there an easy way to do this? Or do we have to do the sorting ourselves?
« Last Edit: February 05, 2023, 05:22:46 pm by loaded »
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

lainz

  • Hero Member
  • *****
  • Posts: 4449
    • https://lainz.github.io/
Re: How to sort the files in the File List Box?
« Reply #1 on: February 05, 2023, 04:38:14 pm »
In your program is sorted but not respecting the numberd sorting. The sorting is an character sorting.

Say 12 is less than 2 in alphabet. Not in numbers...

loaded

  • Hero Member
  • *****
  • Posts: 824
Re: How to sort the files in the File List Box?
« Reply #2 on: February 05, 2023, 05:02:12 pm »
Thanks for the reply lainz
So what should we do so that the name order is the same as in the file explorer? Isn't there an easy way around this?
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: How to sort the files in the File List Box?
« Reply #3 on: February 05, 2023, 05:04:30 pm »
Or do we have to do the sorting ourselves?
Yes... And you can put any degree of complexity into the compare function of your own. Here is a simple one, based on NaturalCompareText of unit StrUtils:
Code: Pascal  [Select][+][-]
  1. uses
  2.   StrUtils;
  3.  
  4. function CompareByNumbers(List: TStringList; Index1, Index2: Integer): Integer;
  5. begin
  6.   Result := NaturalCompareText(List[Index1], List[Index2]);
  7. end;
  8.  
  9. procedure TForm1.FormCreate(Sender: TObject);
  10. var
  11.   L: TStringList;
  12. begin
  13.   FileListbox1.Sorted := false;
  14.   FileListbox1.Directory := '.';
  15.   L := TStringList.Create;
  16.   try
  17.     L.Assign(FileListbox1.Items);
  18.     L.CustomSort(@CompareByNumbers);
  19.     FileListbox1.Items.Assign(L);
  20.   finally
  21.     L.Free;
  22.   end;
  23. end;
  24.  

loaded

  • Hero Member
  • *****
  • Posts: 824
Re: How to sort the files in the File List Box?
« Reply #4 on: February 05, 2023, 05:22:35 pm »
It's spelled wp, it's pronounced King.  :)
Yes, that's what I wanted, thank you very much.
Check out  loaded on Strava
https://www.strava.com/athletes/109391137

 

TinyPortal © 2005-2018