[fpc-pascal] Elegant way to switch sort mechanism in TSortedCollection?

Jim Leonard trixter at oldskool.org
Wed Mar 12 19:51:59 CET 2014


I'm working on a project that utilizes a TSortedCollection to help keep 
items sorted during processing.  There is a need to re-sort the 
collection by different criteria on-demand as the program runs. 
(Different sort orders are an integral part of the processing.)

Unless I'm missing something, TSortedCollection lacks the ability to 
change the Compare method after initialization and/or explicitly re-sort 
the collection on-demand.  I was planning on handling each new (re)sort by:

1. Initializing a new TSortedCollection with the desired Compare method
2. Copying the contents from the old collection to the new one
3. Swap collection pointers; destroy the old collection.

This works without much penalty because the collection will never get 
beyond a reasonable size, but the implementation makes me cringe.  Is 
there a more elegant way to do this (without writing my own sort 
routines for a plain TCollection)?
-- 
Jim Leonard (trixter at oldskool.org)
Check out some trippy MindCandy: http://www.mindcandydvd.com/
A child borne of the home computer wars: http://trixter.oldskool.org/
You're all insane and trying to steal my magic bag!



More information about the fpc-pascal mailing list