Solved

sort, but not as desired

Posted on 2007-03-21
3
290 Views
Last Modified: 2010-04-04
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
Comment
Question by:A. Cristian Csiki
  • 2
3 Comments
 
LVL 11

Author Comment

by:A. Cristian Csiki
ID: 18767297
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
 
LVL 10

Accepted Solution

by:
dinilud earned 500 total points
ID: 18769589
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
 
LVL 11

Author Comment

by:A. Cristian Csiki
ID: 18774482
Thanks.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now