StringGrid Sort

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]

 

Search

Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

 

Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. 

     

We receive a few cents from each purchase.  Thanks

 


Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Mensa Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa 365 Puzzlers  Calendar 2017

Mensa 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only

 

 

Here's a small program that will get you started on sorting a string grid (TStringGrid) by the data in a selected column.

I wanted to make the top grid row a fixed row and preserve it during the sort (i.e. keep it as the top row, regardless of it's values).  It turns out that neither OnClick nor OnCellSelect event exits are triggered by clicks on a cell in a fixed row or column.  So I used an OnMouseUp exit to trigger the  call to the SortGrid procedure. The stringgrid name and column to sort are passed as parameters.   

The sort itself is almost trivial.  For each scrollable row, i, examine the selected column values for all higher row numbers.  If a smaller value is found, swap it with row i.  The slightly tricky part was how to to exchange two rows when necessary.  TCustomGrid is TStringGrid's ancestor  and contains a MoveRow method.  But TStringGrid doesn't declare it so we can't use it.   We could define a new class or component that does include MoveRow,  but that lessens it's generality.  So I just implemented my own code using a temporary TStringList and the Assign method to exchange 2 rows when  necessary.   

The sort could be made more flexible by passing SortGrid a "Column type" field to identify string, integer, floating point, or date value types.   SortGrid would then convert and compare grid string values based on the column type.   For now, I just format the numeric columns with leading blanks to preserve alphabetic sort order and treat everything as strings.

Addendum August 8, 2006:  A small change today in the OnMouseUp routine allows variable width columns to be recognized correctly.  

I should also mention that there is now a GridQuickSort demo program which allows data type as suggested above and use the Quick sort algorithm for much faster sorting of large grids. 

Click here to download the sample GridSort program.

  

 

  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2017, Gary Darby    All rights reserved.