How to override sort mechanism of TClientDataSet
Asked Answered
D

3

5

Is it possible to change the way indexes are used in TClientDataSet to sort records? After reading this question, I thought it would be nice to be able to sort string fields logically in a client dataset. But I have no idea how to override default behavior of client dataset when it comes to indexes. Any ideas?

PS: My CDS is not linked to any provider. I'm looking for a way to modify the sort mechanism of the TClientDataSet (or the parent in which the mechanism is implemented) itself.

Deferral answered 29/7, 2013 at 10:26 Comment(2)
Are you displaying data from CDS to the user? If so, which component are you using? Most Grid- and ListView-type components allow you to implement custom sorting so, if CDS doesn't have what you need, you can try using this instead.Olomouc
@Olomouc I'm using JvDBGrid.Deferral
A
5

You cannot override the sort mechanism of a ClientDataSet - unless you rewrite the according part of Midas.

To achieve the correct sorting (whatever logical means) you can introduce a new field and set its values in a way so that, sorted with the standard mechanism, they will give the required sort order.

Alexisaley answered 29/7, 2013 at 17:42 Comment(6)
Is midas.obj source available? I'm more than willing to change the according parts of it if the source is available and the license agreement allows modifications to it.Deferral
c:\Program Files (x86)\Embarcadero\RAD Studio\9.0\source\data\dsnap\midas\Alexisaley
I don't have the mentioned folder in my XE2 installation folder. :-(Deferral
The midas sources are included since D2010 in Professional and higher. If you have not fiddled with the setup it should be installed.Alexisaley
I'll try reinstalling my XE2Deferral
Found it. Midas source code is installed if you choose to install C++ BuilderDeferral
A
3

Read the excellent on-line article Understanding ClientDataSet Indexes by Cary Jensen.

It explains how to use various ways of sorting and indexing using IndexDefs, IndexFieldNames and IndexName.

Edit: reply to your comment.

You cannot override a sorting method in TClientDataSet, but you can add do this:

If you want to do custom sorting on anything else than existing fields, then you have to add a Calculated Field, perform a kind of order calculation in the OnCalcFields event, then add that field to the IndexDefs.

Analysis answered 29/7, 2013 at 15:38 Comment(4)
I've read the article. I'm looking to override the actual method which compares and positions records according to current index. Something like docwiki.embarcadero.com/Libraries/XE4/en/…Deferral
I have another question regarding using calculated fields and indexes. I have been unable to use internal calculated fields in an index so far: #17896829Deferral
Hmm, that is too bad. I thought I'd done something like this in the past, but I don't remember the project any more. Sorry for that. Can you upload a small (code only) example to something like gist.github.com then I might be able to look into this later this week. Post a comment here when you uploaded the example.Analysis
I have a project running with a fkInternalCalc field and IndexName set to that. It doesn't work for fkCalculated fields, because those are not seen by the midas implementation, which handles the index.Alexisaley
H
1

I would try to achieve the desired sort with an SQL statement that feed the ClientDataSet.

For example if I was dealing with the following strings in FieldN

a_1
a_20
a_10
a_2

and I wanted them sorted like this (I assume this is similar to what you mean by logically

a_1
a_2
a_10
a_20

then I would write the SQL as

SELECT     FieldA, 
           FieldB, 
           ... ,
           FieldN,
           CAST(SUBSTRING(FieldN, 3, 2) TO INTEGER) As FieldM '<== pseudocode
FROM       TableA
ORDER BY   FieldM

The exact syntax of the SubString and Cast to Integer operations will depend on which DBMS you're using.

Horeb answered 29/7, 2013 at 12:54 Comment(6)
My CDS is an in memory dataset.Deferral
All data on a computer is in memory, what's so special about your CDS? :-)Horeb
A ClientDataSet cannot execute SQL.Alexisaley
@UweRaabe I know that! I was suggesting he use an SQL to feed the CDS. I think there are some query components that have a built-in CDS. Anyway, the OP seems quite determined to solve his exact question not necessarily the problem of logical sorting.Horeb
@Horeb My CDS is filled with data coming from a serial port, not from a RDBMSDeferral
@iManBiglari oh ok. All the best.Horeb

© 2022 - 2024 — McMap. All rights reserved.