I'm wanting to reverse the order of an index in a TClientDataSet, the following code looks like it should do the trick but does nothing. Is there a nice way to reverse the order of an index?
procedure TForm8.Button1Click(Sender: TObject);
var
index: TIndexDef;
begin
index := ClientDataSet1.IndexDefs.Find('LengthIndex');
if ixDescending in index.Options then
index.Options := index.Options - [ixDescending]
else
index.Options := index.Options + [ixDescending];
end;
TIndexDef.Options
are used when creating the indexes. They can't be used to try and affect an existing index. See the documentation (emphasis mine):You'll need to create a separate index with the
ixDescending
value set. You can then switch back and forth by just changing theIndexName
property.This is the method I ended up settling on for sorting in both directions. Basically it just creates and frees indexes, not very pretty but works. This is much easier to do with a TFDMemTable (if you have access to FireDAC)