2

A related question about sorting may be relevant to answering this one. I've realized that VirtualTreeView offers a sorting method of it's own, but it seems to work slower than just sorting through the data itself, and letting the GUI refresh.

Is it ever "good practice" to manipulate GUI elements instead of the data that they display, and why?

Community
  • 1
  • 1
programstinator
  • 1,354
  • 3
  • 12
  • 31

2 Answers2

7

Typically you want to maintain a separation between the underlying data and the visual representation of that data. With that in mind, one would typically prefer to implement sorting at the GUI level rather than lower down at the data level.

For example, this allows you to show multiple views of the same data, differently sorted. That's the sort of benefit you reap from maintaining clear separation between model and view.

In your case your implementations of the two options have shown a performance difference. What I would take from that is that it is possible to optimise your sorting when implemented at the GUI level. That's how I would approach the problem.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Upon opening 'VirtualTrees.pas' and looking at what 'sort' does, it looks to me that a lot more work is done to sort visual elements than when sorting data, such as strings or integers. I think that there is no point in trying to rewrite the sort from VirtualTrees. This is why it is slower, and why I asked when would it be good to sacrifice performance for ease of use. – programstinator Oct 02 '12 at 09:29
  • 1
    You don't need to sacrifice performance. You can still have performance, and a clear separation. You just need a layer between the GUI tree view control and the data. That layer maintains the presentation order for a specific view. – David Heffernan Oct 02 '12 at 09:35
  • I though that was what I had, in my program. The data is collected by parsing through a report file, memory leaks are stored in a structure called TMemoryLeak, stored in TMemoryLeakList, which are all stored in a big list of lists TGroupedMemoryLeakList. The nodes of the TVirtualStringTree are the GUI elements. Am I still missing something here? :) – programstinator Oct 02 '12 at 09:40
  • 1
    He probably meant like you can have ClientDataSet or some other in-memory TDataSet, fill it with your leaks and sort it as u like, then use this intermediate dataset to populate VTW – Arioch 'The Oct 02 '12 at 09:47
  • 2
    I meant putting in a layer between `TGroupedMemoryLeakList` and `TVirtualStringTree`. This layer would contain the ordering of the `TGroupedMemoryLeakList` elements associated with a particular view. The ordering is probably nothing more than an `array of Integer` where each `Integer` is an index into the `TGroupedMemoryLeakList`. When the user wants to sort, it's that intermediate layer, the array of indices that is sorted. – David Heffernan Oct 02 '12 at 10:29
2

I would not sort the data, just an index. You say you collect data in lists and these lists are then grouped in a grouplist. Instead of sorting the data, I would leave the data as they are and generate a simple list (or lists) with references to the data and sort that. Use that list to populate the virtual listview.

That would be the kind of intermediate layer David mentions. By keeping several indices, you can sort on different criteria without having to sort the underlying data.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94