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.

Community
  • 1
  • 1
iMan Biglari
  • 4,674
  • 1
  • 38
  • 83
  • 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. – LightBulb Jul 29 '13 at 17:42
  • @LightBulb I'm using JvDBGrid. – iMan Biglari Jul 29 '13 at 20:02

3 Answers3

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.

Uwe Raabe
  • 45,288
  • 3
  • 82
  • 130
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.

Jeroen Wiert Pluimers
  • 23,965
  • 9
  • 74
  • 154
  • I've read the article. I'm looking to override the actual method which compares and positions records according to current index. Something like http://docwiki.embarcadero.com/Libraries/XE4/en/System.Classes.TStringList.CustomSort – iMan Biglari Jul 29 '13 at 15:53
  • I have another question regarding using calculated fields and indexes. I have been unable to use internal calculated fields in an index so far: http://stackoverflow.com/questions/17896329/how-to-sort-a-clientdataset-on-a-calculated-field?rq=1 – iMan Biglari Jul 29 '13 at 20:03
  • 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 https://gist.github.com/ then I might be able to look into this later this week. Post a comment here when you uploaded the example. – Jeroen Wiert Pluimers Jul 29 '13 at 20:06
  • 1
    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. – Uwe Raabe Jul 30 '13 at 05:52
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.

Sam
  • 2,663
  • 10
  • 41
  • 60