Sort the indexpath based keys and values in iOS swift

 Problem:

I had tableview with some set of data and i had a selection option for every indexpath. If user select some indexes in different order but i want the keys to be in indexpath based ascending order.How to achieve that?


Solution:

In the below index selected sort the arrays based on the indexpath with different section and row.

    func sortIndex() {

        let sortedItems = selectedValueIndex.sorted(by: <)

        var newValue:[String] = []

        var newItems:[IndexPath] = sortedItems

        for i in 0..<sortedItems.count {

            

            if let index = selectedValueIndex.firstIndex(of: sortedItems[i]) {

                newValue.append(selectedValue[index])

            }

        }

        selectedValue = newValue

        selectedValueIndex = newItems

    }

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

store cgpoint in userdefaults iOS swift