Sorting a TListview

This is a discussion on Sorting a TListview within the Delphi forums in Programming Languages category; I'm scanning a directory for files and add all files into a TListview with two columns. Column1 holds the file name, Column2 the date on which the file was created. The Listview is sorted by file creation date. Now, there is of course a good chance that several files have the same creation date. How can I most efficiently add the number of occurances of the same file date within the Listview and get the values into a stringlist? Something like '12/24/2007 (3 files)'. The number of files in this folder might be huge, so I probably need to do ...

Go Back   Application Development Forum > Programming Languages > Delphi

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-06-2008, 05:34 PM
Uwe
Guest
 
Default Sorting a TListview

I'm scanning a directory for files and add all files into a TListview with
two columns. Column1 holds the file name, Column2 the date on which the file
was created. The Listview is sorted by file creation date. Now, there is of
course a good chance that several files have the same creation date. How can
I most efficiently add the number of occurances of the same file date within
the Listview and get the values into a stringlist? Something like
'12/24/2007 (3 files)'. The number of files in this folder might be huge, so
I probably need to do various additions simultaniously to speed things up.

Thanks
Uwe

Reply With Quote
  #2  
Old 08-06-2008, 08:44 PM
Keith Latham
Guest
 
Default Re: Sorting a TListview

Uwe wrote:
> I'm scanning a directory for files and add all files into a TListview
> with two columns. Column1 holds the file name, Column2 the date on which
> the file was created. The Listview is sorted by file creation date. Now,
> there is of course a good chance that several files have the same
> creation date. How can I most efficiently add the number of occurances
> of the same file date within the Listview and get the values into a
> stringlist? Something like '12/24/2007 (3 files)'. The number of files
> in this folder might be huge, so I probably need to do various additions
> simultaniously to speed things up.
>
> Thanks
> Uwe
>


My first thought would be to use an inverted list with the date as the
term and the filename as the address converter key.

A simple implementation could use a tHashedStringList for terms and each
term entry would in turn use another tHashedStringList in its associated
object entry for its key occurrences.

So the Inverted list might look something like this

termslist.strings[i] : '12/24/2007' :
termslist.objects[i] => 'file1', 'file2', 'file3'
termslist.strings[j] : '12/25/2007' :
termslist.objects[j] => 'file4', 'file5'

Then the keys tHashedStringList tells you exactly how many files you
have in each term.


quick sketch of method might look something like this (a lot is left out
(destruction, duplicate handling, sorting, etc); this is just to get the
idea) =>

-------------
type tInvertedList = class(tHashedStringList)
public
procedure invertTerm(term,key:string);
end;

procedure invertterm(term,key:string)
var idx : integer;
keys : tHashedStringList;
begin
idx := indexof(term);
if (idx < 0) then begin
keys := tHashedStringList.create;
idx := self.addobject(term,keys);
end;
keys := self.objects[idx];
keys.add(key);
end;

-------------

Then do something like:


var invertedlist : tInvertedList;
var filename : string;
var datestring : string;
var idx : integer;
var occurances : tHashedStringList;
invertedlist := tInvertedList.create;

....
// populate inverted list
PositionAtFirstFile;
while morefiles do begin
datestring := FileDateAsString(CurrentFile);
filename := FileNameOf(CurrentFile);
invertedlist.InvertTerm(datestring,filename);
end;

// populate list view

for idx := 0 to invertedlist.count - 1 do begin
occurances := invertedlist.objects[idx];
datestring := invertedlist[idx] + ' (' +
inttostr(occurances.count) +
' files)';
BuildListViewRows(datestring,occurances);
end;

Using a technique more efficient than a tHashedStringList is left to you
as an exercise (dynamic arrays perhaps?). I usually don't bother as I
find hashed lists quite good enough.

I use inverted lists like this for in memory indexes quite a bit. Often
the key in the occurances list essentially a bookmark to a data record.


Reply With Quote
  #3  
Old 08-06-2008, 09:11 PM
Uwe
Guest
 
Default Re: Sorting a TListview

Hi Keith

Thanks a lot. That gives me something to think about.

Uwe
Reply With Quote
  #4  
Old 08-07-2008, 03:14 AM
Jens Gruschel
Guest
 
Default Re: Sorting a TListview

> The number of files
> in this folder might be huge, so I probably need to do various additions
> simultaniously to speed things up.


Make use of BeginUpdate and EndUpdate to speed things up. If it's still
too slow consider using Mike Lischke's TVirtualTreeView.

--
Jens Gruschel
http://www.pegtop.net
Reply With Quote
  #5  
Old 08-07-2008, 10:59 AM
Jouni Aro
Guest
 
Default Re: Sorting a TListview

Jens Gruschel wrote:
>> The number of files in this folder might be huge, so I probably need
>> to do various additions simultaniously to speed things up.

>
> Make use of BeginUpdate and EndUpdate to speed things up. If it's still
> too slow consider using Mike Lischke's TVirtualTreeView.


Or use TListView.OwnerData, which makes TListView virtual. See the help
for more info.

BR, Jouni
Reply With Quote
  #6  
Old 08-07-2008, 11:16 AM
Uwe
Guest
 
Default Re: Sorting a TListview

Hi Jens

I'm using both already... ;-)

The reason why I was asking is that the folder can contain thousands of
files, and I'm not only retrieving file dates but also some metadata. The
latter can be painstakingly slow, so I really need to find a way to do
simultanious sorts on a larger number of attributes/metadata first, before I
can add up the number of occurances. I might go with Keith's proposal, but I
will also check out if an in-memory-table is faster.

Thx
Uwe




"Jens Gruschel" <nospam@thisurldoesnotexist.com> wrote in message
news:489aa051@newsgroups.borland.com...
>> The number of files in this folder might be huge, so I probably need to
>> do various additions simultaniously to speed things up.

>
> Make use of BeginUpdate and EndUpdate to speed things up. If it's still
> too slow consider using Mike Lischke's TVirtualTreeView.
>
> --
> Jens Gruschel
> http://www.pegtop.net


Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 09:04 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.