0

in matrix(StringGrid) NxM sort the elements of each row in nondecreasing order?

var
  Form1: TForm1;
  n,m:integer;
  I:integer;

implementation

{$R *.dfm}

procedure TForm1.btNapraviClick(Sender: TObject);
begin
  with StringGrid1 do
  begin
    n:=StrToInt(edN.text)+1;
    m:=StrToInt(edM.text)+1;
    ColCount:=n;
    RowCount:=m;

    for I:=0 to n-1 do Cells[I,0]:=IntToStr(I);
    for I:=1 to m-1 do Cells[0,I]:=IntToStr(I);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var J,P,K:integer;
begin
  with StringGrid1 do
  begin
    for I:=1 to n do
      for J:=1 to m-1 do
        for K:=J+1 to m do
        begin
          if StrToInt(Cells[I,J]) <= StrToInt(Cells[I,K]) then
          begin
            P:=StrToInt(Cells[I,J]);
            Cells[I,J]:=(Cells[I,K]);
            Cells[I,K]:=IntToStr(P);
          end;
        end;
  end;
end;
2
  • 2
    Not sure what your question is. Why are you testing for <= instead of just <? if they're equal, you're needlessly swapping two equal values. Also, there's no reason for P to be an integer or do a conversion; just make it a string. Do you know you haven't defined a var I : Integer;? I'd also use the debugger to ensure that m and n aren't zero-based and that you don't have an off-by-one error in those inner loops. Commented Apr 6, 2015 at 22:12
  • you are right for < , my fault. but ive declard I: integer like global variable. Commented Apr 7, 2015 at 6:19

1 Answer 1

3

Each Row in a StringGrid decends from TStrings, so you can assign a row to a TStringList and do a custom sort on that one.

Here is some source code:

First I fill the grid with Random data:

procedure TForm60.FormCreate(Sender: TObject);
var
  i, j: Integer;
begin
  Randomize;

  with StringGrid1 do
  begin
    ColCount := 10;
    RowCount := 10;

    for i := 0 to ColCount - 1 do
      for j := 0 to RowCount - 1 do
        Cells[i, j] := IntToStr(Random(5000));
  end;
end;

Then at Button1.Click I sort each row in descending order:

function StringListSortCompare(List: TStringList; Index1, Index2: Integer): Integer;
begin
  Result := StrToIntDef(List[Index2], 0) - StrToIntDef(List[Index1], 0)
end;

procedure TForm60.Button1Click(Sender: TObject);
var
  i: Integer;
  Buffer: TStringList;
begin
  Buffer := TStringList.Create;
  for i := 0 to StringGrid1.RowCount - 1 do
  begin
    Buffer.Assign(StringGrid1.Rows[i]);
    Buffer.CustomSort(@StringListSortCompare);
    StringGrid1.Rows[i].Assign(Buffer);
  end;
  FreeAndNil(Buffer);
end;

Since I subStract the integer value of List[Index2] from List[Index1] the list becomes sorted descending.

And the result:

Before enter image description here

After enter image description here

After reading your question again I'm not sure if you by "nondecreasing order" mean increasing order. If so just implement the sort procedure like this:

function StringListSortCompare(List: TStringList; Index1, Index2: Integer): Integer;
begin
  Result := StrToIntDef(List[Index1], 0) - StrToIntDef(List[Index2], 0)
end;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.