Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sort FileListBox on Date

Posted on 2002-04-20
6
Medium Priority
?
3,288 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Accepted Solution

by:
alanwhincup earned 300 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

885 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