Recent

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

loaded

  • Hero Member
  • *****
  • Posts: 690
[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 »
If Ide=Lazarus 2.2.4 32 Bit and Os=Win 10 Home 64 Bit 22H2 then Get up and do something useful! Because God is the helper of those who start again;

lainz

  • Hero Member
  • *****
  • Posts: 4127
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: 690
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?
If Ide=Lazarus 2.2.4 32 Bit and Os=Win 10 Home 64 Bit 22H2 then Get up and do something useful! Because God is the helper of those who start again;

wp

  • Hero Member
  • *****
  • Posts: 10639
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: 690
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.
If Ide=Lazarus 2.2.4 32 Bit and Os=Win 10 Home 64 Bit 22H2 then Get up and do something useful! Because God is the helper of those who start again;

 

TinyPortal © 2005-2018