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.
-
use an array and a dictionary to do this, or even an array of tuplesReinier Melian– Reinier Melian01/05/2018 08:54:34Commented Jan 5, 2018 at 8:54
-
You are trying to sort an array of what? can you explain a little bit more?Reinier Melian– Reinier Melian01/05/2018 08:56:47Commented 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 otherD-A UK– D-A UK01/05/2018 08:58:44Commented 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 toReinier Melian– Reinier Melian01/05/2018 09:02:47Commented Jan 5, 2018 at 9:02
-
This would mean changing the whole way my code worksD-A UK– D-A UK01/05/2018 09:19:13Commented Jan 5, 2018 at 9:19
2 Answers 2
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.
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"]