* * *

Author Topic: TVirtualStringTree Sort Creating Duplicated Items  (Read 373 times)

pabsungenis

  • Newbie
  • Posts: 3
TVirtualStringTree Sort Creating Duplicated Items
« on: November 15, 2017, 03:58:59 pm »
Trying to migrate an application of mine to use VirtualStringTrees because of the length of time it takes to populate nearly 2000 items into a ListView, and if I use an OwnerData ListView I can't get it to sort.

When the VST has been populated, if I scroll down to the bottom I see the first image.

After trying to sort to reverse the list, I end up with the second image.

Scrolling back, all of the other songs are duplicated over and over until I get down about 150 nodes, at which point they continue normally from that point. See the third image.

The Data record is just a pointer to an existing Object list. The CompareNodes function is here:

Code: Pascal  [Select]
  1. procedure TMusicLibraryTreeForm.MusicLibraryTreeCompareNodes(
  2.   Sender: TBaseVirtualTree; Node1, Node2: PVirtualNode; Column: TColumnIndex;
  3.   var Result: Integer);
  4. var
  5.   Data: PTVTMusicRecord;
  6.   So1, So2: TSong;
  7. begin
  8.   Data := PTVTMusicRecord(TVirtualStringTree(Sender).GetNodeData(Node1));
  9.   So1 := TSong(Data^.Song);
  10.   Data := PTVTMusicRecord(TVirtualStringTree(Sender).GetNodeData(Node2));
  11.   So2 := TSong(Data^.Song);
  12.   Case Column Of
  13.      0: Result := CompareText(So1.CD,So2.CD);
  14.      1: Result := CompareChar(So1.Category,So2.Category,1);
  15.      2: Result := So1.Len - So2.Len;
  16.      3: Result := CompareText(So1.Artist,So2.Artist);
  17.      4: Result := CompareText(So1.Title,So2.Title);
  18.      5: Result := So1.Tempo - So2.Tempo;
  19.    6,7 : Result := CompareDateTime(So1.LastPlay,So2.LastPlay)
  20.   end;
  21. end;    

What am I doing wrong?

Pascal

  • Hero Member
  • *****
  • Posts: 579
Re: TVirtualStringTree Sort Creating Duplicated Items
« Reply #1 on: November 15, 2017, 04:34:40 pm »
1.) How do you initialize the nodes/populate the tree? Is the data in a TList?
2.) IIRC the Result of OnCompareNode needs to be -1, 0 or 1. So So1.Len - So2.Len will not work.
laz trunk - fpc trunk 32bit - Windows 10 Pro x64

pabsungenis

  • Newbie
  • Posts: 3
Re: TVirtualStringTree Sort Creating Duplicated Items
« Reply #2 on: November 15, 2017, 04:36:48 pm »
Problem solved. It wasn't a bug in my coding, it was data corruption. Something fouled up the database I was loading the songs from, giving me over 51000 entries instead of 1887. Back to the backup.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus