How To Sort a StringGrid

any ideas ?

Who is Participating?
ZifNabConnect With a Mentor Commented:

If you want to it yourself, then you can use TStringList's.

Here is some code (not by me, this one explains it already)

Put the columns into TStringLists, put the original order number of the ordered column in the object of that column string list, sort the ordered column, put the ordered column back in the stringgrid, put the unordered column items  against the ordered column items (original order in object of ordered column is                   index into un-ordered column TStringList).

    Like this :-

      Col1StrLst, Col2StrLst : TStringList;
      i : integer;
      {put first column into the strings of a string list}
      Col1StrLst := TStringList.Create;
      with Col1StrLst do begin
      {put the original order number into the objects of the TStrings}
       for i := 0 to Count - 1 do
        Objects[i] := pointer(i); {note the type-cast}
      {now sort the strings - the objects (with references to the
       original position) stay with each string}
        Sorted := true;
    {put ordered strings back into first column}
    {put second column into the strings of a string list}
       Col2StrLst := TStringList.Create;
    {put strings of second column in appropriate row for first column strings - the row number is in the object of the sorted first column}
       for i := 0 to Col0StrLst.Count - 1 do
        StringGrid1.Cells[2, i] :=
          Col2StrLst.Strings[integer(Col1StrLst.Objects[i])];        {note type-cast}
       {free the TStringLists}

The above assumes you have no headings, if you do you would have to save them to variables, set them to blank strings, then sort the rows, then set the heading strings back to the top row. The blank strings in the columns would always sort to the top.

how big are the stringgrids?
ps. why not using great freeware components?

look at supergrid and hypergrid :

All Courses

From novice to tech pro — start learning today.