Custom Stringlist 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

 

 

 

Introduction

"Dictionary" Sort Custom "Numeric" Sort
A1.gif
A10.gif
A100.gif
A100A.gif
A194A.gif
A195.gif
A199.gif
A1A.gif
A1B.gif
A2.gif
A20.gif
A200.gif
A474.gif
A52A.gif
A99.gif
A9A.gif
A1.gif
A1A.gif
A1B.gif
A2.gif
A9A.gif
A10.gif
A20.gif
A52A.gif
A99.gif
A100.gif
A100A.gif
A194A.gif
A195.gif
A199.gif
A200.gif
A474.gif

Here's a program illustrating the use of the "Custom Sort" feature of the Delphi TStringList class. The default TStringList "Sort" method arranges the strings in lexicographic (dictionary) order. The "CustomSort" method passes two index pointers to a user defined function and expects the user function
to return one of three values: -1, +1, or 0 indicating that the first string is "less than", "greater than" or "equal to" the second string.

In this demonstration program, the requirement is to sort a set of file names containing embedded numeric information into sequence accounting for the numeric values. For example, "A100A.xxx" will come before "A11.xxx" in a standard sort but after if numbers are treated as numeric values.

The user function in this case separates each pair of strings into 3 pieces: an alpha prefix, a number, and an alpha suffix. The alphabetic parts are converted to upper case to make the comparison case insensitive. The pieces are compared in sequence with a non-equal result after each piece
determining the returned result.

 

bulletDownload source 
bulletDownload executable

Further Explorations

 
 

 

Created: September 8, 2013

Modified: May 15, 2018

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