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

Date sorted FileList

Hi;

i need a File list sorted by date ascending.

Does anyone knows a component or piece of code i can use for that?

Im using delphi 5.

TIA.
0
arturop
Asked:
arturop
  • 2
1 Solution
 
MotazCommented:
I'm writing code now..
0
 
MotazCommented:
finished..

Drop a ListBox and a Button, and write this code in buttons onClick:

procedure TForm1.Button1Click(Sender: TObject);
var
  Rec: TSearchRec;
  FileList: array of string;
  DateList: array of TDateTime;
  i: Integer;
  Done: Boolean;
  TempName: string;
  TempDate: TDateTime;
begin
  // Get files list
  if FindFirst('c:\*.*', faAnyFile, Rec) = 0 then
  repeat
    Setlength(FileList, Length(FileList) + 1);
    Setlength(DateList, Length(DateList) + 1);
    FileList[High(FileList)]:= Rec.Name;
    DateList[High(DateList)]:= FileDateToDateTime(Rec.Time);
  until FindNext(Rec) <> 0;
  FindClose(Rec);

  // Sort
  // Bubble sort
  repeat
    Done:= True;
    for i:= 0 to High(FileList) - 1 do
      if DateList[i] > DateList[i + 1] then
      begin
        Done:= False;
        TempName:= FileList[i];
        FileList[i]:= FileList[i + 1];
        FileList[i + 1]:= TempName;

        TempDate:= DateList[i];
        DateList[i]:= DateList[i + 1];
        DateList[i + 1]:= TempDate;

      end;

  until Done;

  // Show in list
  ListBox1.Clear;
  for i:= 0 to High(FileList) do
    ListBox1.Items.Add(FileList[i] + '   ' + DateTimeToStr(DateList[i]));
end;


If you want descending sort then invert the comparison operator to <

      if DateList[i] < DateList[i + 1] then


Motaz
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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