[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 304
  • Last Modified:

sort, but not as desired

Sort fine, but in my stringgrid i've got even empty spaces. When I sort the stringgrid the empty rows become before the data from my text.
ex:

a a a a a
b b b b b
'' ''  ''  '' ''

''-empty cell.....means that i've got an empty row.

after sort... the empty row is the first as normaly...but how can I prevent that and put the empty spaces last?
Thanks.
0
A. Cristian Csiki
Asked:
A. Cristian Csiki
  • 2
1 Solution
 
A. Cristian CsikiSenior System AdministratorAuthor Commented:
I forgot to show the code:

procedure SortStringGrid(var GenStrGrid: TStringGrid; ThatCol: Integer);
const
  TheSeparator = '@';
var
  CountItem, I, J, K, ThePosition: integer;
  MyList: TStringList;
  MyString, TempString: string;
begin
  CountItem := GenStrGrid.RowCount;
  MyList        := TStringList.Create;
  MyList.Sorted := False;
  try
    begin
      for I := 1 to (CountItem - 1) do
        MyList.Add(GenStrGrid.Rows[I].Strings[ThatCol] + TheSeparator +
          GenStrGrid.Rows[I].Text);
      Mylist.Sort;
      for K := 1 to Mylist.Count do
      begin
        MyString := MyList.Strings[(K - 1)];
        ThePosition := Pos(TheSeparator, MyString);
        TempString  := '';
        {Eliminate the Text of the column on which we have sorted the StringGrid}
        TempString := Copy(MyString, (ThePosition + 1), Length(MyString));
        MyList.Strings[(K - 1)] := '';
        MyList.Strings[(K - 1)] := TempString;
      end;
      for J := 1 to (CountItem - 1) do
        GenStrGrid.Rows[J].Text := MyList.Strings[(J - 1)];
    end;
  finally
    MyList.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SortStringGrid(StringGrid1, 1);
end;

from http://www.delphitricks.com/source-code/components/sort_a_stringgrid.html
0
 
diniludCommented:
procedure TForm1.SortStringGrid(var GenStrGrid: TStringGrid; ThatCol: Integer);
const
  TheSeparator = '@';
var
  CountItem, I, J, K, ThePosition: integer;
  MyList: TStringList;
  MyString, TempString: string;
  BlankRow:string;
begin
  CountItem := GenStrGrid.RowCount;

  BlankRow:='';
  for i:=0 to GenStrGrid.ColCount-1 do BlankRow:=BlankRow+#13#10;

  MyList        := TStringList.Create;
  MyList.Sorted := False;
  try
    begin
      for I := 1 to (CountItem - 1) do
      begin
        if GenStrGrid.Rows[I].Text<>BlankRow then
           MyList.Add(GenStrGrid.Rows[I].Strings[ThatCol] + TheSeparator +
                                                      GenStrGrid.Rows[I].Text);
      end;
      Mylist.Sort;
      for K := 1 to Mylist.Count do
      begin
        MyString := MyList.Strings[(K - 1)];
        ThePosition := Pos(TheSeparator, MyString);
        TempString  := '';
        {Eliminate the Text of the column on which we have sorted the StringGrid}
        TempString := Copy(MyString, (ThePosition + 1), Length(MyString));
        MyList.Strings[(K - 1)] := '';
        MyList.Strings[(K - 1)] := TempString;
      end;
      for J := 1 to MyList.Count do
        GenStrGrid.Rows[J].Text := MyList.Strings[(J - 1)];

      for j:=MyList.Count+1 to (CountItem - 1) do GenStrGrid.Rows[J].Text :=BlankRow;
    end;
  finally
    MyList.Free;
  end;
end;
0
 
A. Cristian CsikiSenior System AdministratorAuthor Commented:
Thanks.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now