在delphi中对stringgrid中的两列进行排序 [英] Sorting two column in stringgrid in delphi
本文介绍了在delphi中对stringgrid中的两列进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含3列的字符串网格; Year,Value1&值2。我想对降序年份列进行排序(选择最小的一年)。如果最小的年份有超过1个数据(例如:2000,2000,2000),那么我需要选择哪一个是高在哪个值1与年份对应。价值3也一样。
我的问题是,如何选择附件的结果?
登录 - Google帐户 [ ^ ]
我尝试了什么:
这就是我试图对一列进行排序:
I have a string grid with 3 column;Year,Value1 & Value2. I want to sort descending Year column(pick the smallest year). If the smallest Year had more than 1 data(example :2000,2000,2000), so I need to choose which one is HIGHER in value1 correspond with Year. Same also with Value 3.
My question is, how do I pick the result like the attachment?
Sign in - Google Accounts[^]
What I have tried:
this is what I have tried to sort one column:
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
Buffer: TStringList;
begin
Buffer := TStringList.Create;
for i := 0 to StringGrid1.ColCount - 1 do
begin
Buffer.Assign(StringGrid1.Cols[0]);
Buffer.CustomSort(@StringListSortCompare);
StringGrid1.Cols[0].Assign(Buffer);
end;
FreeAndNil(Buffer);
end;
function StringListSortCompare(List: TStringList; Index1, Index2: Integer): Integer;
begin
Result := StrToIntDef(List[Index2], 0) - StrToIntDef(List[Index1], 0)
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i, j: Integer;
begin
Randomize;
with StringGrid1 do
begin
ColCount := 3;
RowCount := 6;
for i := 0 to ColCount - 1 do
for j := 0 to RowCount - 1 do
//Cells[i, j] := IntToStr(Random(5000));
stringgrid1.Cols[0].Add('Year');
stringgrid1.Cells[0,1]:='2000';
stringgrid1.Cells[0,2]:='2001';
stringgrid1.Cells[0,3]:='2000';
stringgrid1.Cells[0,4]:='2002';
stringgrid1.Cells[0,5]:='2000';
stringgrid1.Cols[1].Add('Pcorr');
stringgrid1.Cells[1,1]:='15.6';
stringgrid1.Cells[1,2]:='15.7';
stringgrid1.Cells[1,3]:='15.9';
stringgrid1.Cells[1,4]:='15.9';
stringgrid1.Cells[1,5]:='15.9';
stringgrid1.Cols[2].Add('DD');
stringgrid1.Cells[2,1]:='7.3';
stringgrid1.Cells[2,2]:='7.2';
stringgrid1.Cells[2,3]:='7.0';
stringgrid1.Cells[2,4]:='7.5';
stringgrid1.Cells[2,5]:='7.6';
end;
end;
推荐答案
我没点击提供的链接 - 抱歉。
但快速谷歌搜索确实提供了许多解决方案:在delphi中对stringgrid中的两列进行排序 - Google搜索 [ ^ ]如:
* 按列排序TStringGrid? - Delphi技巧 - CJC Delphi(酷Delphi技巧) [ ^ ]
* delphi - 按行排序TStringGrid及其整数值 - Stack Overflow [ ^ ]
* Delphi按整数列排序stringgrid [ ^ ]
* [已解决]排序字符串网格超过1列 [ ^ ]
* SwissDelphiCenter.ch:...按列对TStringGrid进行排序? [ ^ ]
* StringGrid Sort [ ^ ]
* Delphi源代码 - 如何对StringGrid进行排序 [ ^ ]
* 以及更多! [ ^ ]
I'm not clicking on the link provided - sorry.
But a quick Google Search does turn up a number of solutions: Sorting two column in stringgrid in delphi - Google Search[^] like:
* Sort a TStringGrid by Columns? - Delphi Tips - CJC Delphi (Cool Delphi Tips)[^]
* delphi - Sort TStringGrid by row and its integer value - Stack Overflow[^]
* Delphi sort stringgrid by integer column[^]
* [Solved] Sort String Grid more than 1 column[^]
* SwissDelphiCenter.ch : ...Sort a TStringGrid by Columns?[^]
* StringGrid Sort[^]
* Delphi Source Code - How to sort a StringGrid[^]
* and more![^]
这篇关于在delphi中对stringgrid中的两列进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文