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.

Rob
441k74 gold badges846 silver badges1.1k bronze badges
asked Jan 5, 2018 at 8:52
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.

answered Jan 5, 2018 at 9:23
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"]
answered Jan 5, 2018 at 9:03
2
  • This is amazing thank you, except I want it to sort in descending order - how do I do this? Commented Jan 5, 2018 at 9:17
  • Never mind, did: 1ドル.0 < 0ドル.0, instead of 0ドル.0 < 1ドル.0 Commented Jan 5, 2018 at 9:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.