Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

TStringGrid.sorted

Posted on 2001-07-24
9
Medium Priority
?
207 Views
Last Modified: 2010-04-06
It's all in the StringGrid so now I need to sort it by column 0.
The problem is it's pretending it doesn't know anything about TStringGrid.sorted := true;
Even TStringGrid.please.sortby.Col(0) won't work.

0
Comment
Question by:frog
[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
9 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6312257
Procedure SortGrid(AGrid : TStringGrid; ColIndex : Integer; SortOrder : Boolean);
//SortOrder=true->Ascending,SortOrder=False->Descending
//works only, if there is no comma in a cell
//first row is excluded from the sort
var
  SL : TStringList;
  I  : Integer;
begin
  if (assigned(AGrid)) and
     (ColIndex > -1) and
     (ColIndex < AGrid.ColCount) then
  begin
    SL := TStringList.Create;
    try
      SL.Sorted := true;
      For I := 0 to AGrid.RowCount - 1 do
        SL.Add(AGrid.Cells[ColIndex,I]+'|'+AGrid.Rows[i].CommaText);
      Case SortOrder of
        True : For I := 0 to SL.Count - 1 do
                 AGrid.Rows[I].CommaText := Copy(SL[I],pos('|',SL[I])+1,MaxLongInt);
       False : For I := SL.Count - 1 downto 0 do
                 AGrid.Rows[SL.Count  - I].CommaText := Copy(SL[I],pos('|',SL[I])+1,MaxLongInt);
      end;
    finally
      sl.free;
    end;
  end;
end;


//Sort third col, asc
procedure TForm1.Button1Click(Sender: TObject);
begin
  SortGrid(StringGrid1,2,True);
end;

//Sort secon col desc
procedure TForm1.Button2Click(Sender: TObject);
begin
  SortGrid(StringGrid1,1,False);
end;

meikl


0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6312350
another sample, without the comma-restriction is also available, if needed
0
 
LVL 3

Expert Comment

by:VSF
ID: 6314321
Can you post it here?!?!

Thanks!
VSF
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 6314579
well, here it is, including also a sample of a customsortroutine

unit stringgridsort_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    CheckBox1: TCheckBox;
    procedure StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button1Click(Sender: TObject);
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
  private
    FSortedColIndex : Integer;
    FSortOrder      : Boolean;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

Var AsIntegerFlag : Boolean;

Function SpecialSort(List: TStringList; Index1, Index2: Integer): Integer;
begin
  Result := 0;
  try
    case AsIntegerFlag of
      //AnsiSort
      False : If List[Index1] < List[Index2] then Result := -1
              else If List[Index1] > List[Index2] then Result := 1;
      //IntegerSort
      True  : If StrToInt(List[Index1]) < StrToInt(List[Index2]) then Result := -1
              else If StrToInt(List[Index1]) > StrToInt(List[Index2]) then Result := 1;
    end;
  except
    Result := 0;  //No Sort on Error;
  end;
end;


Procedure SortGrid(AGrid : TStringGrid; ColIndex : Integer; SortOrder : Boolean;
                   //Optional Parameters
                   UseCustomSort : Boolean = False;
                   CustomSortProc : TStringListSortCompare = Nil);
//SortOrder=true->Ascending,SortOrder=False->Descending
//first row is excluded from the sort
var
  SL : TStringList;
  ARow : TStringList;
  I  : Integer;
begin
  if (assigned(AGrid)) and
     (ColIndex > -1) and
     (ColIndex < AGrid.ColCount) then
  begin
     SL := TStringList.Create;
    try
      For I := 1 to AGrid.RowCount - 1 do
      begin
        ARow := TStringList.Create;
        ARow.Assign(AGrid.Rows[i]);
        SL.AddObject(AGrid.Cells[ColIndex,I],ARow);
      end;
      if useCustomSort and assigned(CustomSortProc) then
      try
        SL.CustomSort(CustomSortProc);  //Userdefined sort
      except
        raise;
      end
      else
        SL.Sorted := true; //AnsiCompareString
      Case SortOrder of
        True : For I := 0 to SL.Count - 1 do
                 if Assigned(SL.Objects[I]) then
                   AGrid.Rows[I+1].Assign(TStringList(SL.Objects[I]));
       False : For I := SL.Count - 1  downto 0 do
                 if Assigned(SL.Objects[I]) then
                   AGrid.Rows[AGrid.RowCount - I - 1].Assign(TStringList(SL.Objects[I]));
      end;
    finally
      for I := 0 to sl.count-1 do
        if Assigned(SL.Objects[I]) then
        begin
          TStringList(SL.Objects[I]).Free;
          SL.Objects[I] := Nil;
        end;
      sl.free;
    end;
  end;
end;

Function RandomString(Lngth : Integer; CharSet : TSysCharset) : String;
var C : Char;
begin
  Result := '';
  if (lngth > 0) and not (CharSet = []) then
    while length(Result) < lngth do
    begin
      Repeat
        C := Chr(Random(255));
      Until C in CharSet;
      Result := Result + C;
    end;
end;



procedure TForm1.StringGrid1MouseDown(Sender: TObject;
 Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  ACol,ARow : Integer;
  ASortProc : TStringListSortCompare;
begin
  ASortProc := SpecialSort;
  StringGrid1.MouseToCell(x,y,ACol,ARow);
  AsIntegerFlag := ACol in [1,2];
  if (ARow = 0) and
     (ACol > 0) and
     (Acol < StringGrid1.RowCount) then
    if Button =  mbleft then
    begin
      FSortedColIndex := ACol;
      FSortOrder := True;
      SortGrid(StringGrid1,ACol,True,Checkbox1.Checked,ASortProc)  //asc on columntitle-leftclick
    end
    else
    begin
      FSortedColIndex := ACol;
      FSortOrder := False;
      SortGrid(StringGrid1,ACol,False,Checkbox1.Checked,ASortProc);//Desc on columntitle-rightclick
    end;
end;

//Create a unsorted content
procedure TForm1.Button1Click(Sender: TObject);
var I : Integer;
begin
  StringGrid1.RowCount := 10001;
  StringGrid1.ColCount := 7;
  for i := 1 to 6 do
    StringGrid1.Cells[I,0] := Randomstring(5,['0'..'9','A'..'Z','a'..'z']);
  for i := 1 to 10000 do
  begin
    StringGrid1.Cells[1,I] := IntToStr(I);
    StringGrid1.Cells[2,I] := IntToStr(10001-I);
    StringGrid1.Cells[3,I] := Randomstring(10,['A'..'Z']);
    StringGrid1.Cells[4,I] := Randomstring(10,['0'..'9','A'..'Z','a'..'z']);
    StringGrid1.Cells[5,I] := Randomstring(10,[#0..#255]);
    StringGrid1.Cells[6,I] := '1000';
  end;
end;


//Draw a triangle
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
const CMargin = 5;
begin
  If (ARow = 0) and (ACol = FSortedColIndex) then  //FSortedColIndex->the col, which is sorted
  case FSortOrder of
    true : begin  //Ascending
             StringGrid1.Canvas.Pen.Color := clbtnShadow;
             StringGrid1.Canvas.MoveTo(Rect.Right-CMargin,Rect.Top+CMargin);
             StringGrid1.Canvas.LineTo(Rect.Right-7-CMargin,Rect.Top+CMargin);
             StringGrid1.Canvas.LineTo(Rect.Right-4-CMargin,Rect.Top+7+CMargin);
             StringGrid1.Canvas.Pen.Color := clBtnHighLight;
             StringGrid1.Canvas.LineTo(Rect.Right-CMargin,Rect.Top+CMargin);
           end;
   false : begin  //Descending
             StringGrid1.Canvas.Pen.Color := clBtnHighLight;
             StringGrid1.Canvas.MoveTo(Rect.Right-4-CMargin,Rect.Top+CMargin);
             StringGrid1.Canvas.LineTo(Rect.Right-CMargin,Rect.Top+7+CMargin);
             StringGrid1.Canvas.LineTo(Rect.Right-7-CMargin,Rect.Top+7+CMargin);
             StringGrid1.Canvas.Pen.Color := clbtnShadow;
             StringGrid1.Canvas.LineTo(Rect.Right-4-CMargin,Rect.Top+CMargin);
           end;
  end;
end;

initialization
  Randomize;
end.

meikl ;-)
0
 

Accepted Solution

by:
RUSTAM earned 200 total points
ID: 6316129
Every column and row in TStringGris are TStringList. Thjat's why you can use this sample:

StringGrid1.Columns[0].Sorted := True;
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6316147
this would cause that the rows becomes mixed,
because only the column is sorted but not the row

sample effect

unsorted
2 4
1 5
7 6
4 8

sorted by the proposed answer
1 4
2 5
4 6
7 8

sorted by my sample(s)
1 5
2 4
4 8
7 6

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6316173
just to say RUSTOM its not a good fair style to answer a q,
if there are already comments of a solution are given.

specially that (i guess) your answer isn't the solution, which is needed, except the grid does have only one column

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6316174
RUSTOM->RUSTAM

sorry, for the typo :-)
0
 

Author Comment

by:frog
ID: 6317064
The way I did it was similar to that suggested by Meikl, but because the grid was a fixed 4 columns wide and only needed to sort A-Z on column 0 it didn't have to be as flexible. (Just a few 'quick & dirty' lines of inflexible code.)
It seemed to be so crude and an unnecessary complication so I was looking for an elegant one liner that must surely exist, because as Rustam points out the StringGrid code is probably only an array of stringLists.
I was hoping that instead of doing something odd like removing the sort, Borland just forgot to include the syntax in the help system. ie, it just needed someone in the know to reveal the syntax of how to access it and still maintain the integrity of the rows.
I agree Meikl, that locking an answer with previous comments un-assessed is unusual, perhaps Rustam is new to EE.
I looks like I'll have to keep my existing messy code so I'll accept the answer and post points to meikl as well.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

688 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