Check out the TStringList.CustomSort method. You supply a customised Compare function so you can sort using any method you like.
For a similar but different approach you could look at the TurboPower SysTools library (opensource at sourceforge) which contains a Tsort object. You stuff items in then get them back in sorted order. You also supply a customised compare function. Wes Edwards -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Chris Veale Sent: Monday, 28 July 2003 9: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/ --------------------------------------------------------------------------- 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/
