Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

sorting a stringgrid

I would like to sort my stringgrid into the correct alphabetical order of the items in the second column. So far i have the following code which sorts the items in the second column, but i do not know how to make it sort the whole row as opposed to only the items in the 2nd column. As a result, the infomation in my stringgrid becomes jumbled up. Could anybody help me in this?

[code]
procedure TForm1.BtnSortClick(Sender: TObject);
begin
for i:=1 to 8 do
for j:=0 to 7 do
if (form1.stringgrid1.cells[2,j]) > (form1.stringgrid1.cells[2,j+1])then
begin
tempstorage:=(form1.stringgrid1.cells[2,j+1]);
form1.stringgrid1.cells[2,j+1]:=(form1.stringgrid1.cells[2,j]);
form1.stringgrid1.cells[2,j]:=tempstorage;
end;
end;
[/code]

Comments

  • zibadianzibadian Member Posts: 6,349
    : I would like to sort my stringgrid into the correct alphabetical order of the items in the second column. So far i have the following code which sorts the items in the second column, but i do not know how to make it sort the whole row as opposed to only the items in the 2nd column. As a result, the infomation in my stringgrid becomes jumbled up. Could anybody help me in this?
    :
    : [code]
    : procedure TForm1.BtnSortClick(Sender: TObject);
    : begin
    : for i:=1 to 8 do
    : for j:=0 to 7 do
    : if (form1.stringgrid1.cells[2,j]) > (form1.stringgrid1.cells[2,j+1])then
    : begin
    : tempstorage:=(form1.stringgrid1.cells[2,j+1]);
    : form1.stringgrid1.cells[2,j+1]:=(form1.stringgrid1.cells[2,j]);
    : form1.stringgrid1.cells[2,j]:=tempstorage;
    : end;
    : end;
    : [/code]
    :
    Here is an untested code based on your code. The bold parts are add to your code:
    : [code]
    : procedure TForm1.BtnSortClick(Sender: TObject);
    : begin
    : for i:=1 to 8 do
    : for j:=0 to 7 do
    : if (form1.stringgrid1.cells[2,j]) > (form1.stringgrid1.cells[2,j+1])then
    : begin
    [b] for col := 0 to stringgrid1.ColCount-1 do
    begin // switch the entire row 1 string at a time[/b]
    tempstorage:=(form1.stringgrid1.cells[[b]col[/b],j+1]);
    : form1.stringgrid1.cells[[b]col[/b],j+1]:=(form1.stringgrid1.cells[[b]col[/b],j]);
    : form1.stringgrid1.cells[[b]col[/b],j]:=tempstorage;
    [b] end[/b]
    : end;
    : end;
    : [/code]

  • Justin BibJustin Bib USAMember Posts: 0

    ___ || http://forcoder.org || free video tutorials and ebooks about \ Visual Basic .NET, Swift, Perl, Ruby, Scratch, Go, Assembly, Objective-C, C, C++, Delphi, Visual Basic, Python, JavaScript, MATLAB, Java, C#, R, PHP, PL/SQL FoxPro, Erlang, Lua, ABAP, Alice, Transact-SQL, Hack, COBOL, ML, Kotlin, Rust, Prolog, Fortran, Julia, Scheme, VBScript, Lisp, F#, Clojure, LabVIEW, D, Ada, Bash, Dart, Awk, Logo, SAS, Apex, Scala, Crystal \ ____

Sign In or Register to comment.