When you call
sorted(<#source: C#>, <#isOrderedBefore: (C.Generator.Element, C.Generator.Element) -> Bool##(C.Generator.Element, C.Generator.Element) -> Bool#>)
you can access the two source elements for comparison via $0
and $1
. But how can I determine the index from where they were taken from the source?
You could pass into
sorted
not a collection of elements, but the indices of the elements:Or, if you don't like capturing
a
and wanted the elements themselves passed into the closure, you could pass in a sequence of pairs of the index and the element, like so:Note the result would be an array of
(index,element)
pairs:[(3, be), (4, going), (0, hello), (1, i), (2, must)]
. If you wanted to turn that back into just the elements, you can domap(pairs) { $0.1 }
Also, if you take the just-indices route and want to turn that back into elements later, you can do it with
PermutationGenerator
: