<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html><body>
<p>using the example at http://acm.msu.ru/mkoshp/fpc/rtl/objects/tsortedcollection.html</p>
<p>may something this way ...</p>
<pre><span class="kw">Type<br /></span>TMethod = ( method_type_1, method_type_2, method_type_3); </pre>
<pre>PMySortedCollection <span class="sym">=</span> <span class="sym">^</span>TMySortedCollection<span class="sym">;</span> TMySortedCollection <span class="sym">=</span> <span class="kw">Object</span><span class="sym">(</span>TSortedCollection<span class="sym">)<br /> method : TMethod;<br /></span> <span class="kw">Function</span> Compare <span class="sym">(</span>Key1<span class="sym">,</span>Key2 <span class="sym">:</span> Pointer<span class="sym">)</span><span class="sym">:</span> Sw_integer<span class="sym">;</span> <span class="kw">virtual</span><span class="sym">;</span> <br /> procedure setMethod( const m : TMethod);<br /><span class="kw">end</span><span class="sym">;</span> <br /><br /><span class="kw">Implementation</span> <br /><br /><span class="kw">Uses</span> MyObject<span class="sym">;</span> <br /><br /><span class="kw">Function</span> TMySortedCollection<span class="sym">.</span>Compare <span class="sym">(</span>Key1<span class="sym">,</span>Key2 <span class="sym">:</span> Pointer<span class="sym">)</span> <span class="sym">:</span>sw_integer<span class="sym">;</span> <br /><br /><span class="kw">begin</span> <br />       case method of</pre>
<pre>      method_type_1: Compare<span class="sym">:=</span>PMyobject<span class="sym">(</span>Key1<span class="sym">)</span><span class="sym">^</span><span class="sym">.</span>GetField <span class="sym">-</span> PMyObject<span class="sym">(</span>Key2<span class="sym">)</span><span class="sym">^</span><span class="sym">.</span>GetField<span class="sym">;</span></pre>
<pre>      method_type_2: Compare<span class="sym">:=</span>PMyobject<span class="sym">(</span>Key2<span class="sym">)</span><span class="sym">^</span><span class="sym">.</span>GetField <span class="sym">-</span> PMyObject<span class="sym">(</span>Key1<span class="sym">)</span><span class="sym">^</span><span class="sym">.</span>GetField<span class="sym">;</span></pre>
<pre>      method_type_3: Compare<span class="sym">:=</span>PMyobject<span class="sym">(</span>Key1<span class="sym">)</span><span class="sym">^</span><span class="sym">.</span>GetField <span class="sym">- 2 *</span> PMyObject<span class="sym">(</span>Key2<span class="sym">)</span><span class="sym">^</span><span class="sym">.</span>GetField<span class="sym">;</span> </pre>
<pre>      end;<br /><span class="kw">end</span><span class="sym">;<br /><br /></span></pre>
<pre>procedure TMySortedCollection<span class="sym">.</span>setMethod( const m : TMethod);</pre>
<p><span class="sym">begin</span></p>
<p><span class="sym">      method := m; </span></p>
<p>      ....</p>
<p>end;</p>
<p> </p>
<p>Em 12.03.2014 15:51, Jim Leonard escreveu:</p>
<blockquote type="cite" style="padding-left:5px; border-left:#1010ff 2px solid; margin-left:5px; width:100%"><!-- html ignored --><!-- head ignored --><!-- meta ignored -->
<pre>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)?</pre>
</blockquote>
<p> </p>
</body></html>