user645976
user645976

Reputation:

Alpha sort stringgrid

How to alpha sort a stringgrid on a form on a given column in Delphi?

Upvotes: 1

Views: 1276

Answers (1)

David Heffernan
David Heffernan

Reputation: 613612

There's no built in sort facility for TStringGrid, so you need to roll your own. Personally, I use some general purpose sorting code that can sort anything provided a compare function and an exchange function:

type
  TCompareIndicesFunction = function(Index1, Index2: Integer): Integer of object;
  TExchangeIndicesProcedure = procedure(Index1, Index2: Integer) of object;

procedure Sort(const First, Last: Integer; Compare: TCompareIndicesFunction; Exchange: TExchangeIndicesProcedure);
begin
  //insert search algorithm here
end;

You could look at how Generics.Collections.TArray.Quicksort is implemented to see how to fill in the missing code above.

The essential point is that your Compare and Exchange functions, which are methods of objects, contain the knowledge of how to compare items in the string grid, and then how to swap them.

Upvotes: 1

Related Questions