Skip to main content
5 of 7
added 404 characters in body

Counting sort in Swift

I have the below sorting algorithm which takes an array of dictionary values as declared below:

guard var imageUrlString = anyImage.value as? [String:AnyObject] else { return }

Value:

 ["media1": {
    mediaUrl = "URL";
    postTimeStamp = "573889189.73954";
    timeStamp = "573889179.6991431"; }, "media4": {
    mediaUrl = "URL";
    timeStamp = "573889185.750419"; }, "media2": {
    mediaUrl = "URL";
    timeStamp = "573889181.49576"; }, "media3": {
    mediaUrl = "URL";
    timeStamp = "573889183.89598"; 
}]

I then loop through it adding the values with the smallest Int key to a new array to be used afterward. Thus the values in this array of AnyObjects would go from 1 to n.

I was wondering if I could make it more concise.

var values = [AnyObject]()
var keys = [String]()
var Done = false
var j = 1

 while !Done {
    for i in imageUrlString {
        let key = Int(String(i.key.last!))
        
        if j == key {
            values.append(i.value)
            keys.append(i.key)
            print(i, " This is the i for in if ")
            if imageUrlString.count == j {
                print("Done yet: yes", values[0], " ", values[3])
                Done = true
                break;
            }
            j+=1
        } else {
            print("No,,.")
        }
    }
}