Sorting [String : Int] pair alphabetically, even if there are duplicates

I've got a dictionary of type [String : Int] that I need to create a sorted list from. The String is the name of the object, and the Int is the ID. I need to be able to sort the objects by their name alphbetically, but I can only reference them by their ID because two objects can be named the same thing, but the IDs will never be the same. Is there a way to turn that dictionary into an [Int] array of IDs so that if I were to pull the objects from these IDs they would be in alphabetical order? I also need it to be able to work with objects with the same name, which is really tripping me up at the moment. How can I do this?

Accepted Reply

Why not create a dictionary where the key is the ID (Int) and the value the name (String).


In addition, you cannot have 2 entries in a dict with the same key, so I do not understand how your case is possible.


You could also create an array of pairs (name: String, id: Int) that you can sort by names.

Replies

Why not create a dictionary where the key is the ID (Int) and the value the name (String).


In addition, you cannot have 2 entries in a dict with the same key, so I do not understand how your case is possible.


You could also create an array of pairs (name: String, id: Int) that you can sort by names.