Boost C++ Libraries: Ticket #2807: list::sort and slist::sort are not stable https://svn.boost.org/trac10/ticket/2807 <p> The intrusive::list::sort method does not preserve the relative order of equivalent elements. In fact, it makes it the reverse. </p> <p> The following code snippet shows the problem: </p> <pre class="wiki">#include &lt;iostream&gt; #include &lt;boost/checked_delete.hpp&gt; #include &lt;boost/intrusive/options.hpp&gt; #include &lt;boost/intrusive/list.hpp&gt; #include &lt;boost/intrusive/list_hook.hpp&gt; namespace bi = boost::intrusive; typedef bi::list_base_hook&lt; bi::link_mode&lt; bi::auto_unlink &gt; &gt; BaseHook_t; struct MyData : public BaseHook_t { struct OrderByKey { typedef bool result_type; result_type operator() (MyData const&amp; left, MyData const&amp; right) const { return left.m_Key &lt; right.m_Key; } }; int m_Key; int m_Data; explicit MyData(int k, int d) : m_Key(k), m_Data(d) {} }; int main(int, char*[]) { typedef bi::list&lt; MyData, bi::base_hook&lt; BaseHook_t &gt;, bi::constant_time_size&lt; false &gt; &gt; List_t; List_t l; MyData* p = 0; p = new MyData(1, 11); l.push_back(*p); p = new MyData(1, 12); l.push_back(*p); p = new MyData(1, 13); l.push_back(*p); p = new MyData(1, 14); l.push_back(*p); p = new MyData(3, 31); l.push_back(*p); p = new MyData(3, 32); l.push_back(*p); p = new MyData(3, 33); l.push_back(*p); p = new MyData(3, 34); l.push_back(*p); p = new MyData(2, 21); l.push_back(*p); p = new MyData(2, 22); l.push_back(*p); p = new MyData(2, 23); l.push_back(*p); p = new MyData(2, 24); l.push_back(*p); l.sort(MyData::OrderByKey()); for (List_t::const_iterator it = l.begin(); it != l.end(); ++it) { std::cout &lt;&lt; it-&gt;m_Data &lt;&lt; ", "; } std::cout &lt;&lt; std::endl; l.clear_and_dispose(boost::checked_deleter&lt; MyData &gt;()); return 0; } </pre><p> It prints: </p> <pre class="wiki">14, 13, 12, 11, 24, 23, 22, 21, 34, 33, 32, 31, </pre><p> The expected output is: </p> <pre class="wiki">11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34, </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2807 Trac 1.4.3 Andrey Semashev Fri, 27 Feb 2009 18:16:22 GMT attachment set https://svn.boost.org/trac10/ticket/2807 https://svn.boost.org/trac10/ticket/2807 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">list.hpp.patch</span> </li> </ul> <p> The patch that fixes the problem (against branches/release) </p> Ticket Andrey Semashev Fri, 27 Feb 2009 18:16:53 GMT type changed https://svn.boost.org/trac10/ticket/2807#comment:1 https://svn.boost.org/trac10/ticket/2807#comment:1 <ul> <li><strong>type</strong> <span class="trac-field-old">Bugs</span> → <span class="trac-field-new">Patches</span> </li> </ul> Ticket Andrey Semashev Fri, 27 Feb 2009 18:38:59 GMT summary changed https://svn.boost.org/trac10/ticket/2807#comment:2 https://svn.boost.org/trac10/ticket/2807#comment:2 <ul> <li><strong>summary</strong> <span class="trac-field-old">list::sort is not stable</span> → <span class="trac-field-new">list::sort and slist::sort are not stable</span> </li> </ul> <p> The slist::sort function has precisely the same problem. </p> Ticket Andrey Semashev Fri, 27 Feb 2009 18:39:47 GMT attachment set https://svn.boost.org/trac10/ticket/2807 https://svn.boost.org/trac10/ticket/2807 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">slist.hpp.patch</span> </li> </ul> <p> The patch that fixes the problem (against branches/release) </p> Ticket Ion Gaztañaga Wed, 25 Mar 2009 17:55:29 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/2807#comment:3 https://svn.boost.org/trac10/ticket/2807#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> Applied a modified patch. Revision 51971. </p> Ticket