<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi Taras,<div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 11 Apr 2016, at 08:48, Taras Zakharko via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">The implementation should be fairly straightforward. E.g. here is an extension method I use:</div><div class=""><br class=""></div><div class=""><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">extension</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="font-variant-ligatures: no-common-ligatures;">CollectionType</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">func</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> order(</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">@noescape</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> isOrderedBefore: (</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">Self</span><span class="" style="font-variant-ligatures: no-common-ligatures;">.Generator.Element, </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">Self</span><span class="" style="font-variant-ligatures: no-common-ligatures;">.Generator.Element) -> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">Bool</span><span class="" style="font-variant-ligatures: no-common-ligatures;">) -> [</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">Self</span><span class="" style="font-variant-ligatures: no-common-ligatures;">.Index] {</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">return</span><span class="" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">indices</span><span class="" style="font-variant-ligatures: no-common-ligatures;">.</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(61, 29, 129);">sort</span><span class="" style="font-variant-ligatures: no-common-ligatures;">({ isOrderedBefore(</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">self</span><span class="" style="font-variant-ligatures: no-common-ligatures;">[$0], </span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">self</span><span class="" style="font-variant-ligatures: no-common-ligatures;">[$1]) })</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures;"> }</span></div><div class="" style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;">}</div></div></div></blockquote></div><br class=""></div><div class="">Just a side note that you could also:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(73, 162, 210);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(200, 172, 229);" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> </span><span style="font-variant-ligatures: no-common-ligatures;" class="">SequenceType</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class=""> {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span></span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(200, 172, 229);" class="">func</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> order(</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(200, 172, 229);" class="">@noescape</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> isOrderedBefore: (Generator.Element, Generator.Element) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(73, 162, 210);" class="">Bool</span><span style="font-variant-ligatures: no-common-ligatures;" class="">) -> [</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(73, 162, 210);" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures;" class="">] {</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span></span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(200, 172, 229);" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(145, 84, 15);" class="">enumerate</span><span style="font-variant-ligatures: no-common-ligatures;" class="">().</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(145, 84, 15);" class="">sort</span><span style="font-variant-ligatures: no-common-ligatures;" class="">{ isOrderedBefore($0.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(255, 38, 0);" class="">1</span><span style="font-variant-ligatures: no-common-ligatures;" class="">, $1.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(255, 38, 0);" class="">1</span><span style="font-variant-ligatures: no-common-ligatures;" class="">) }.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(145, 84, 15);" class="">map</span><span style="font-variant-ligatures: no-common-ligatures;" class="">{ $0.</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(255, 38, 0);" class="">0</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> }</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro';" class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">}</span></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; min-height: 15px;" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""></span><br class=""></div><div style="margin: 0px; font-size: 12px; line-height: normal; font-family: 'Source Code Pro'; color: rgb(203, 203, 203);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(255, 38, 0);" class="">0</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">...</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(255, 38, 0);" class="">3</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">).</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(145, 84, 15);" class="">reverse</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">().</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(145, 84, 15);" class="">order</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(0, 0, 0);" class="">(<) </span><span style="font-variant-ligatures: no-common-ligatures;" class="">// [3, 2, 1, 0]</span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">This way you can `order` all sequences, and it is more efficient as you don’t fetch elements by index inside the `isOrderedBefore`. (You could also *not* `map` at the end and return all the elements along with their original indexes.)</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">milos</span></div></body></html>