Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

Delphi
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;
Posted
Updated 13-Sep-17 16:25pm
v2

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900