Yes, it is possible. Just use the indexes offered by Dataset.
I find it interesting to talk a little about the indices:
There are basically two types of indices in Delphi: The temporary and the persistent.
Indices temporary are created using Indexfieldnames. Let’s assume you have a Clientdataset with Fields: Primeironome, Ultimonome and Datanascimento. If I wanted to sort my records by last name and then by first, I could do that:
ClientDataSet1.IndexFieldNames := 'UltimoNome;PrimeiroNome';
At any time this can be changed.
ClientDataSet1.IndexFieldNames := 'UltimoNome';
// etc...
When you do this Delphi creates the indices internally and associates them to the dataset. That’s why they’re called temporary.
In your case this will not suit you, I wrote only for enrichment. In your case an index persistent will be more appropriate:
Indexes persistent are created in the Indexdefs of your Dataset. So you can, for each index, define a number of properties. They are used in the same way as indexes temporary, just use the name given to the index persistent when they are created in design-time.
To create an Index Persistent in Runtime, just use the code snippet below:
with ClientDataSet1.IndexDefs.AddIndexDef do
begin
Name := 'OrdenarUltimoPrimeiroNomedx';
Fields := 'UltimoNome;PrimeiroNome';
Options := [ixDescending, ixCaseInsensitive];
end;
ClientDataSet1.IndexName := 'OrdenarUltimoPrimeiroNomedx';
EDIT: I talked so much and ended up not giving an exact answer.
You can create as many persistent indexes as you need:
with ClientDataSet1.IndexDefs.AddIndexDef do
begin
Name := 'OrdenarUltimoPrimeiroNomedx';
Fields := 'UltimoNome;PrimeiroNome';
Options := [ixDescending, ixCaseInsensitive];
end;
with ClientDataSet1.IndexDefs.AddIndexDef do
begin
Name := 'DataNascimentoAscdx';
Fields := 'DataNascimento';
Options := [ixAscending];
end;
ClientDataSet1.IndexName := 'DataNascimentoAscdx;OrdenarUltimoPrimeiroNomedx';
In the above example I will sort by birth date Ascendant and then by Last Name and First Descending Name.
If you want a good reference, I suggest you access this link:
Thank you. I just had to explain how to manage to order some fields in the ascending order and others as decreasing. yet with your explanation I was able to do here. Following your last example, if I want you to order the surname in descending order, but the name in ascending order would look like this: <! -- language: lang-delphi -->
 with ClientDataSet1.IndexDefs.AddIndexDef do 
 begin 
 Name := 'OrdenarNomedx'; 
 Fields := 'UltimoNome;PrimeiroNome';
 descFields := 'UltimoNome'
 Options := [ixDescending]; end; Clientdataset1.Indexname := 'Ordinancex';
– Stribus
@Stribus really I could give a attention to this. I edited the answer to make it clearer how to solve your problem. Thanks for the touch.
– Edgar Muniz Berlinck
Man, congratulations, it was great, accurate and satisfactory your answer.
– user14920
Options := [ixAscending]; Does not accept this property!
– user24425