Solved

sort, but not as desired

Posted on 2007-03-21
3
297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi Yen format 3 61
how to resize animated Gif image in delphi ? 1 45
firemonkey keyboard covers the controls 1 52
Delphi: barcode reading on android platform 1 72
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

749 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