Solved

Sort FileListBox on Date

Posted on 2002-04-20
6
2,952 Views
Last Modified: 2008-03-06
I want to sort the files in a FileListBox on their date of creation. So most recent first. Now the files are sorted alfabetically by name. How can I accompish this simple task.
0
Comment
Question by:detempel
6 Comments
 
LVL 5

Expert Comment

by:alanwhincup
ID: 6955880
A TFileListBox is effectively read-only so you would have to use a normal TListbox to display the sorted filenames. Heris an example below:

uses
  ..., FileCtrl, ...

function FileDateString(const FileName : string) : string;
begin
  Result := FormatDateTime('yyyymmdd hhnnss|',
                            FileDateToDateTime(FileAge(FileName)));
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  SL : TStringList;
  Path : string;
  I : Integer;
begin
  SL := TStringList.Create;
  try
    Path := FileListBox1.Directory + '\';
    for I := 0 to FileListBox1.items.count -1 do
      SL.Add(FileDateString(Path + FileListBox1.Items[I]) +
              FileListBox1.Items[I]);
      SL.Sorted := True;
      SL.Sorted := False;
      {Items cannot be changed on a sorted list, so mark it
       unsorted again. This does not change the order of the items.}
    for I := 0 to SL.Count -1 do
      SL[I] := Copy(SL[I], 17, MaxInt);
    ListBox1.Items.Assign(SL);
  finally
    SL.free;
  end;
end;

Cheers,

Alan
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6975648
This questions is reasonably current, others below are not and all of them need your attention.  ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if they are still open in 14 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20163261.html
http://www.experts-exchange.com/questions/Q.20269573.html
http://www.experts-exchange.com/questions/Q.20282042.html
http://www.experts-exchange.com/questions/Q.20282960.html
http://www.experts-exchange.com/questions/Q.20291699.html



*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643
POINTS FOR EXPERTS awaiting comments are listed in the link below
http://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @14 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thanks everyone.
Moondancer
Moderator @ Experts Exchange
0
 

Author Comment

by:detempel
ID: 6975658
I have tried and it works great. The list is sorted alphabetically so the oldest files shows up first. Instead of this I want the most recent file first. I have solved this like this:

    for I := SL.Count - 1 downto 0 do
    SL2.Add(SL[I]);

Now I have a FileListBox of 12.577 files. The FileListBox fills in about 4 seconds. When I sort the FileListBox into a ListBox the sorting-process (sort, fill and display) takes about 52 seconds. I understand this is a big sort but is it possible to reduce the sorting-time in some other way?

Erik.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 5

Accepted Solution

by:
alanwhincup earned 100 total points
ID: 6977833
Theirs not really alot you could do about this because it has to access each file to get it's date of creation whilst a FileListbox just has to display files. The speed will also be degraded by the number of items which you are displaying in the listbox. The above code I did can be improved slightly to the below:

procedure TForm1.Button1Click(Sender: TObject);
var
 SL : TStringList;
 Path : string;
 I : Integer;
begin
 SL := TStringList.Create;
 try
   Path := FileListBox1.Directory + '\';
   for I := 0 to FileListBox1.items.count -1 do
     SL.Add(FileDateString(Path + FileListBox1.Items[I]) +
             FileListBox1.Items[I]);
     SL.Sorted := True;
     SL.Sorted := False;
     {Items cannot be changed on a sorted list, so mark it
      unsorted again. This does not change the order of the items.}
   for I := 0 to SL.Count -1 do
     ListBox1.Items.Add(Copy(SL[I], 17, MaxInt));
 finally
   SL.free;
 end;
end;

This may make a bit of a difference on a huge number of items. If you want even better performance then maybe you could store the list in a TList instead of a StringList and then call TList.Sort with a compare function. Delphi already uses the QuickSort method within the TStringList but calling TStringList.Sort does not use a Compare
function, but is virtual.

I can't try to implement the TList idea quite yet as I don't have Delphi with me at the moment but will have a go as soon as I get home.

Cheers,

Alan
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9003732
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept answer from alanwhincup

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Paul (pnh73)
EE Cleanup Volunteer
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9010301
Thankyou for your response.

Paul (pnh73)
EE Cleanup Volunteer
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now