Pregunta

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.

¿Fue útil?

Solución

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.

Otros consejos

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.

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top