1

I have an array of integers that represent their own strings.

I know there are simple ways to sort integer arrays, but I needed to make it so that the integers retain their correspondence to the strings.

Here is my current code:

var i = 0
var highestValueObjectInArray = 0
for object in createTimerData.speechTimeStamps {

    if object > highestValueObjectInArray {
        highestValueObjectInArray = object
    }

}

while i + 1 < createTimerData.speechTimeStamps.count {

    if createTimerData.speechTimeStamps[i] < createTimerData.speechTimeStamps[i + 1] {

        let TS2 = createTimerData.speechTimeStamps[i + 1]
        let TSS2 = createTimerData.speechText[i + 1]

        createTimerData.speechTimeStamps.remove(at: i + 1)
        createTimerData.speechTimeStamps.insert(TS2, at: i)
        createTimerData.speechText.remove(at: i + 1)
        createTimerData.speechText.insert(TSS2, at: i)

    }

    i += 1
    if i+1 >= createTimerData.speechTimeStamps.count {

        var lastItem = highestValueObjectInArray + 1
        var inDescendingOrder = true
        for object in createTimerData.speechTimeStamps {
            if object < lastItem {} else { inDescendingOrder = false }
            lastItem = object
        }
        if inDescendingOrder == false {
            i = 0
        }
    }
}

It is very slow and not very efficient so when it sorts large arrays it takes a large amount of time. Is there a way I have overlooked, or a way that is more efficient.

Any help would be much appreciated, thanks.

6
  • use an array and a dictionary to do this, or even an array of tuples Commented Jan 5, 2018 at 8:54
  • You are trying to sort an array of what? can you explain a little bit more? Commented Jan 5, 2018 at 8:56
  • An array of integers, and I want the strings array to follow the same sorting, so that the integers and strings still match each other Commented Jan 5, 2018 at 8:58
  • you can do this using an array of tuples (int,String), I can provide some example if you need to Commented Jan 5, 2018 at 9:02
  • This would mean changing the whole way my code works Commented Jan 5, 2018 at 9:19

2 Answers 2

3

Often, when you want to sort separate arrays alongside each other, you should really consider merging them into a single array of a custom type. For example, speechText and speechTimestamps might merged into an array of a single type, Speech:

struct Speech { 
    let text: String
    let timestamp: Int
}

let speeches = [
    Speech(text: "Baz", timestamp: 2),
    Speech(text: "Bar", timestamp: 3),
    Speech(text: "Foo", timestamp: 1)
]

Then you can sort these as you see fit:

let result = speeches.sorted { $0.timestamp < $1.timestamp }

Clearly, modify the types and the names as appropriate, but hopefully this will illustrate the idea. Consider a single array of a type that captures both things being sorted.

Sign up to request clarification or add additional context in comments.

Comments

2

You can zip the two arrays together, sort the zipped array, and then separate them like below

let arr1 = [1,4,2,5,3]
let arr2 = ["One","Four","Two","Five","Three"]

let combined = zip(arr1, arr2).sorted(by: {
    $0.0 < $1.0
})

let sorted1 = combined.map {$0.0}
let sorted2 = combined.map {$0.1}

print(sorted1) // [1, 2, 3, 4, 5]
print(sorted2) // ["One", "Two", "Three", "Four", "Five"]

2 Comments

This is amazing thank you, except I want it to sort in descending order - how do I do this?
Never mind, did: $1.0 < $0.0, instead of $0.0 < $1.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.