Swift sort an array and return an array of index and element

Viewed 2042

I wish to sort an array, and return an array of indexes and elements within the sorted array;

ie:

var filtered = self.existingOrders.sorted(by: { (eo1:ExistingOrder, eo2:ExistingOrder) -> Bool in
                return (eo1.value < eo2.value)
            }).index(0, offsetBy: 0)

This just gives me an index of a specific number.

I would like a sorted array of elements and indexes; so I can grab the necessary index and do manipulations on them.

How do I make it so I can do;

// pseduocode:

var filtered:(Index, Element) = self.existingOrders.sortByLowest.return(flatMap(index, element))

or possibily even chain an enumerator to help give me a list of all items indexed; post-sort?

ie:

 let filtered = self.existingOrders.sorted(by: { (eo1:ExistingOrder, eo2:ExistingOrder) -> Bool in
                return (eo1.value < eo2.value)
            }).enumerated().flatMap({ (offset:Int, element:ExistingOrder) -> (Int, ExistingOrder) in
                return (offset, element)
            })

I wish to get a sorted array and return an index and element to my filter variable.

How do I accomplish this?

Many thanks

1 Answers
Related