I have a StringGrid in my form and when I press the Button1, I move some cells inside this grid. Here's an example:

When I press the Button1, I go from the situation A to the situation B. But I'd like to be able to do the reverse thing too: I mean that I'd like to go from B to A when I press another button called Button2.
I'd like to create something like an "undo" button. How could I do this? I am using Lazarus.
I need to do only 1 undo. Here's the procedure that moves the StringGrid's cells:
procedure TForm1.SortGrid(Grid : TStringGrid; const SortCol:integer; const datatype:integer; const ascending:boolean);
var
i : integer;
tempgrid:tstringGrid;
list:array of integer;
begin
tempgrid:=TStringgrid.create(self);
with tempgrid do
begin
rowcount:=grid.rowcount;
colcount:=grid.colcount;
fixedrows:=grid.fixedrows;
end;
with Grid do
begin
setlength(list,rowcount-fixedrows);
for i:= fixedrows to rowcount-1 do
begin
list[i-fixedrows]:=i;
tempgrid.rows[i].assign(grid.rows[i]);
end;
//Call the procedure and sort the stuff
Quicksort(Grid, list,0,rowcount-fixedrows-1,sortcol,datatype, ascending);
for i:=0 to rowcount-fixedrows-1 do
begin
rows[i+fixedrows].assign(tempgrid.rows[list[i]])
end;
row:=fixedrows;
end;
tempgrid.free;
setlength(list,0);
screen.cursor:=crdefault;
end;
And when I click the Button1...
SortGrid(StringGrid1,1,1,true);