Skip to content

Instantly share code, notes, and snippets.

@freeonterminate
Last active February 7, 2018 08:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freeonterminate/1202732a0ec58aa20ba60095a79619f1 to your computer and use it in GitHub Desktop.
Save freeonterminate/1202732a0ec58aa20ba60095a79619f1 to your computer and use it in GitHub Desktop.
TStringGrid をソートするサンプルコード
(*
* TStringGrid をソートするサンプルコード
*
* Copyright (c) 2018 HOSOKAWA Jun.
*
* HOW TO USE:
* 1. Add PK.StringGrid.Helper to uses block.
* 2. Call StringGrid.SortByColumn(Column)
*
* EXAMPLE:
* uses
* PK.StringGrid.Helper;
*
* procedure TForm1.FormCreate(Sender: TObject);
* begin
* StringGrid1.RowCount := 4;
*
* StringGrid1.Cells[0, 0] := '222';
* StringGrid1.Cells[0, 1] := '333';
* StringGrid1.Cells[0, 2] := '111';
* StringGrid1.Cells[0, 3] := '000';
*
* StringGrid1.Cells[1, 0] := 'ddd';
* StringGrid1.Cells[1, 1] := 'ccc';
* StringGrid1.Cells[1, 2] := 'aaa';
* StringGrid1.Cells[1, 3] := 'bbb';
*
* StringGrid1.Cells[2, 0] := 'DDD';
* StringGrid1.Cells[2, 1] := 'BBB';
* StringGrid1.Cells[2, 2] := 'CCC';
* StringGrid1.Cells[2, 3] := 'AAA';
* end;
*
* procedure TForm1.StringGrid1HeaderClick(Column: TColumn);
* begin
* StringGrid1.SortByColumn(Column);
* end;
*
* LICENSE:
* 本ソフトウェアは「現状のまま」で、明示であるか暗黙であるかを問わず、
* 何らの保証もなく提供されます。
* 本ソフトウェアの使用によって生じるいかなる損害についても、
* 作者は一切の責任を負わないものとします。
*
* 以下の制限に従う限り、商用アプリケーションを含めて、本ソフトウェアを
* 任意の目的に使用し、自由に改変して再頒布することをすべての人に許可します。
*
* 1. 本ソフトウェアの出自について虚偽の表示をしてはなりません。
* あなたがオリジナルのソフトウェアを作成したと主張してはなりません。
* あなたが本ソフトウェアを製品内で使用する場合、製品の文書に謝辞を入れて
* いただければ幸いですが、必須ではありません。
*
* 2. ソースを変更した場合は、そのことを明示しなければなりません。
* オリジナルのソフトウェアであるという虚偽の表示をしてはなりません。
*
* 3. ソースの頒布物から、この表示を削除したり、表示の内容を変更したりしては
* なりません。
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute
* it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented;
* you must not claim that you wrote the original software.
* If you use this software in a product, an acknowledgment in the product
* documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such,
* and must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*)
unit PK.StringGrid.Helper;
interface
uses
FMX.Grid;
type
TStringGridHelper = class helper for TStringGrid
public
procedure SortByColumnNo(const iCol: Integer);
procedure SortByColumn(const iColumn: TColumn);
end;
implementation
uses
System.SysUtils;
{ TStringGridHelper }
procedure TStringGridHelper.SortByColumnNo(const iCol: Integer);
procedure QuickSort(iLo, iHi: Integer);
var
Min, Max, Mid: Integer;
tmpStr: String;
i: Integer;
begin
repeat
Min := iLo;
Max := iHi;
Mid := (iLo + iHi) shr 1;
repeat
while CompareStr(Cells[iCol, Min], Cells[iCol, Mid]) < 0 do
Inc(Min);
while CompareStr(Cells[iCol, Max], Cells[iCol, Mid]) > 0 do
Dec(Max);
if (Min <= Max) then
begin
for i := 0 to ColumnCount - 1 do
begin
tmpStr := Cells[i, Min];
Cells[i, Min] := Cells[i, Max];
Cells[i, Max] := tmpStr;
end;
if (Mid = Min) then
Mid := Max
else if (Mid = Max) then
Mid := Min;
Inc(Min);
Dec(Max);
end;
until (Min > Max);
if (iLo < Max) then
QuickSort(iLo, Max);
iLo := Min;
until (Min >= iHi);
end;
begin
QuickSort(0, RowCount - 1);
end;
procedure TStringGridHelper.SortByColumn(const iColumn: TColumn);
var
Col: Integer;
i: Integer;
begin
// ソート元になるコラムを探す
Col := -1;
for i := 0 to ColumnCount - 1 do
if (Columns[i] = iColumn) then
begin
Col := i;
Break;
end;
if (Col < 0) then
Exit;
SortByColumnNo(Col);
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment