ObjectDataSet - allow sorting by any object

Issue #269 wontfix
VlaPr created an issue

The code currently do not support sorting by any object. The procedure CompareRecords is virtual, but uses private variables that disallow to implement own sorting (that's why I have classified this as a bug).

It can be solved for example adding an onCompareRecord event where user can implement his own rules ...

function TObjectDataSet.CompareRecords(const left, right: TObject): Integer;
var
  i: Integer;
  fieldInfo: TIndexFieldInfo;
  leftValue, rightValue: TValue;
begin
  Result := 0;

  for i := 0 to High(fIndexFields) do
  begin
    fieldInfo := fIndexFields[i];
    leftValue := fieldInfo.Prop.GetValue(left);
    rightValue := fieldInfo.Prop.GetValue(right);

    // Added new Event onCompareRecord
    if Assigned(fonCompareRecord) then
    begin
      Result := fonCompareRecord(leftValue, rightValue);
      if fieldInfo.Descending then
        Result := -Result;
      if Result <> 0 then
        Exit;
    end;
    //

    Result := leftValue.CompareTo(rightValue);
    if fieldInfo.Descending then
      Result := -Result;

    if Result <> 0 then
      Exit;
  end;
end;

Comments (2)

  1. Stefan Glienke repo owner

    Sorting on the TObjectDataSet itself is still subject to change (or even removed as most UI controls like grids have their own sorting functionality).

  2. Log in to comment