Hi
I have used the below code to change numbers and dates to strings so that
they sort correctly. The large number 2000000000 obviously has to be bigger
than the largest number that you are going to use. I use 200... so if the
numbers are negative it still works ok. Dates work by converting then to a
number and then sorted as per numbers. Probably not as neat as using custom
sort but quite a bit easier.
Warren.S
sl:=TStringList.Create;
for r:=1 to rc-1 do begin
s1:=Cells[SortCol,r];
try s1:=FloatToStr(StrToDate(s1)); except end;
try s1:='0'+FloatToStrF(2000000000+StrToFloat(s1),ffFixed,18,4);
except end;
sl.AddObject(UpperCase(s1+' '+Cells[0,r]),TObject(r));
end;
sl.Sort;
ps
In my origional code procedure TForm1.StringGrid1 should have been
TForm1.StringGrid1MouseDown
I assume that you figured that
-----Original Message-----
From: Chris Veale [mailto:[EMAIL PROTECTED]
Sent: Monday, 28 July 2003 09:39 a.m.
To: Multiple recipients of list delphi
Subject: RE: [DUG]: tstringgrid sorting.
thanks for that Warren it works like a charm...except.
I am trying to sort float values (in scientific notation) and consequently
the sort doesnt work.
for instance
1.0E-13 is sorted before
5.0E2 is sorted before
7.83E-28
ie dont based on string value of the cell contents and not the float...
I tried converting it to a float format of 0.00000234 but this still doesnt
work.
Is it because Im using them as strings?
is there another way to quickly sort a tstringgrid contents based on a float
value? or would I be better to load an ayyar with the stringgrid contents.
Is there a way to walk the array and load a second (sorted) array?
Cheers
Chris Veale
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> Behalf Of Warren Slater (ASL)
> Sent: Thursday, 24 July 2003 5:15 p.m.
> To: Multiple recipients of list delphi
> Subject: RE: [DUG]: tstringgrid sorting.
>
>
> Hi
> I have used the code below to allow sorting a stringgrid by
> clicking on the
> column header. Clicking again alternates between ascending
> and descending.
> The current sorted column is stored in the tag field of the
> stringgrid. I
> have deleted a few lines that are not required but should
> compile as below.
> Warren
>
>
> procedure TForm1.StringGrid1(Sender: TObject;
> Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
> var
> r,SortCol : Integer;
> begin
> with TStringGrid(Sender) do begin
> MouseToCell(x,y,SortCol,r);
> if r=0 then begin
> if (SortCol+1)=Tag then
> Tag:=-(SortCol+1)
> else
> Tag:=(SortCol+1);
> SortStringGrid(Sender);
> end;
> end;
> end;
>
>
> procedure SortStringGrid(Sender: TObject);
> var
> r,c,SortCol,rc,cc : Integer;
> sl : TStringList;
> s : array of array of String;
> begin
> with TStringGrid(Sender) do
> if Tag<>0 then begin
> // ignore blank lines
> rc:=RowCount;
> cc:=ColCount;
> while (rc>1) and (Cells[0,rc-1]='') do
> Dec(rc);
> SortCol:=Abs(Tag)-1;
> SetLength(s,cc,rc);
> SetLength(b,rc);
> // copy cells to sort to stringlist
> sl:=TStringList.Create;
> for r:=1 to rc-1 do
> sl.AddObject(UpperCase(Cells[SortCol,r]+'
> '+Cells[0,r]),TObject(r));
> sl.Sort;
> // copy stringgrid data to temporary array
> for r:=1 to rc-1 do
> for c:=0 to cc-1 do
> s[c,r]:=Cells[c,r];
> // copy data back in sorted order
> if Tag<0 then begin
> for r:=1 to rc-1 do
> for c:=0 to cc-1 do
> Cells[c,rc-r]:=s[c,Integer(sl.Objects[r-1])];
> end
> else begin
> for r:=1 to rc-1 do
> for c:=0 to cc-1 do
> Cells[c,r]:=s[c,Integer(sl.Objects[r-1])];
> end;
> sl.Free;
> end;
> end;
>
>
> -----Original Message-----
> From: Chris Veale [mailto:[EMAIL PROTECTED]
> Sent: Thursday, 24 July 2003 02:43 p.m.
> To: Multiple recipients of list delphi
> Subject: [DUG]: tstringgrid sorting.
>
>
> Hi all.
>
> I want to sort a tstringgrid in ascending order of one column
> but want to
> also order the rest of the columns also (change the order of the rows)
>
> Is there a function or something to do this or will I have to
> write a custom
> sort tool myself?
>
> Id prefer something like the tlistbox sort procedure but the
> difficulty
> comes when I want to organise the remainder of the columns also.
>
> Cheers
>
> Chris Veale
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.502 / Virus Database: 300 - Release Date: 18/07/03
>
>
>
>
> ______________________________________________________
> The contents of this e-mail are privileged and/or confidential to the
> named recipient and are not to be used by any other person and/or
> organisation. If you have received this e-mail in error,
> please notify
> the sender and delete all material pertaining to this e-mail.
> ______________________________________________________
> --------------------------------------------------------------
> -------------
> New Zealand Delphi Users group - Delphi List -
> [EMAIL PROTECTED]
> Website: http://www.delphi.org.nz
> To UnSub, send email to: [EMAIL PROTECTED]
> with body of "unsubscribe delphi"
> Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/
>
> ##############################################################
> #######################
> This e-mail message has been scanned for Viruses and Content
> and cleared
> by MailMarshal
> For more information please visit www.marshalsoftware.com
> ##############################################################
> #######################
> --------------------------------------------------------------
> -------------
> New Zealand Delphi Users group - Delphi List -
> [EMAIL PROTECTED]
> Website: http://www.delphi.org.nz
> To UnSub, send email to: [EMAIL PROTECTED]
> with body of "unsubscribe delphi"
> Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/
> ---
> Incoming mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.502 / Virus Database: 300 - Release Date: 18/07/03
>
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.504 / Virus Database: 302 - Release Date: 24/07/03
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED]
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/
#####################################################################################
This e-mail message has been scanned for Viruses and Content and cleared
by MailMarshal
For more information please visit www.marshalsoftware.com
#####################################################################################
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED]
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/