<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Swift standard library already offers a useful set of sort() functions. However, it is also often useful to know how the collection should be rearranged in order to become sorted. For example, R defines the order() function which returns a permutation of collection indexes which rearrange the collection in an order. I suggest to add similar functionality to the Swift Collections E.g.:<div class=""><br class=""></div><div class="">var out = []</div><div class="">for i in collection.order({$0 < $1}) { out.append(collection[i]) }</div><div class="">// out is now sorted collection</div><div class=""><br class=""></div><div class="">Knowing the sort order is useful in many applications where the data cannot or should not be rearranged and yet some information about the ordering is helpful, e.g. for traversing the collection in a specific way. It is also helpful for maintaining multiple ordering relations associated with the same collection. </div><div class=""><br class=""></div><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 style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">CollectionType</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> order(</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">@noescape</span><span style="font-variant-ligatures: no-common-ligatures" class=""> isOrderedBefore: (</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Self</span><span style="font-variant-ligatures: no-common-ligatures" class="">.Generator.Element, </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Self</span><span style="font-variant-ligatures: no-common-ligatures" class="">.Generator.Element) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Bool</span><span style="font-variant-ligatures: no-common-ligatures" class="">) -> [</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Self</span><span style="font-variant-ligatures: no-common-ligatures" class="">.Index] {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">indices</span><span style="font-variant-ligatures: no-common-ligatures" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">sort</span><span style="font-variant-ligatures: no-common-ligatures" class="">({ isOrderedBefore(</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">[$0], </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">[$1]) })</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> }</span></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">}</p></div><div class=""><br class=""></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class="">Best, </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=""> Taras</span></div><div class=""><br class=""></div></body></html>