1

Is there a way that I can sort an array based on an alphabetical sort that I did to another array? The practical use to this would be trying to associate an array with usernames to an array that contains those usernames' names. I have two separate lists containing this information and I need to alphabetically sort the users based on their name, but I need to access their username too. So in order to do this I'm trying to have both pieces of data within the table view cell and just show the name and hide the username.

I can't reverse sort the array containing the names to the usernames because if two people had the same names this would cause many problems.

I tried using a NSMutableDictionary but this didn't work. Any help would be extremely helpful!

2 Answers 2

2

I have two separate lists containing this information

There's your problem. Don't use two data structures to store parts of one object.

Create a model object that represents a user and includes both the identifier and the name. Then everything is easy. The entire object is tiny:

@interface User
@property (nonatomic, readwrite, copy) NSString *identifier;
@property (nonatomic, readwrite, copy) NSString *name;
@end
@implementation
@end

Sorting objects by properties is simple.

Even if other parts of the system didn't use this object, I'd still create it as a "view model" object just for this view controller. (But it's probably better to treat it as a regular model object and use it throughout your system.)

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

3 Comments

This does makes much more sense then working with two arrays. When you say create a model object, can you provide more information on how to do that?
The above implementation is a model object. It just means "an object that holds data." A "view model" object is just one that holds data specifically to make things easy on views that display them. Sometimes they're organized a little differently (might hold attributed strings or screen coordinates), but it's just a specialized version of a model object. The MVC pattern is one of the core pieces of Cocoa development. developer.apple.com/library/ios/documentation/General/…
The key point is that you have a model which holds all the data, views which display the data, and controllers that coordinate between the two. View Controllers should never hold data. They just hook up views that need the data to models that hold it. (A fancier version called MVVM has started to become popular, but it's just a further extension on this idea, adding view models as an explicit rather than "we just often do that" layer.)
0

I'm not sure what you are really asking here.

But let me have a crack at this. Assuming you have an alphabetically sorted array of usernames. From that, why dont you create a NSHastable with <key,value>=<username,name>.

You also wont have the problem where two names will conflict with each other, assuming your usernames are unique.

Comments

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.