Searching through Arrays (most efficient method)

To save people from reading too much, this is the “abbreviated” version, while the next post will contain the “full version” in it’s original glory.

Here is the deal. I have a “User” class with 4 properties, all Strings. I want to store those instances inside of a “UserList” class. The UserList class has several properties to sort through and find users.

Now, at least the “getUserByPrefix” and “getUserByNickname” require to retrieve values very quickly, so (I’m guessing) that options like this are** not a good idea**:

function getUserByPrefix(prefix:String):User
{
   for (var i:int = 0; i < userArray; i++)
   {
      if (userArray*.prefix == prefix) 
         { return userArray*; }
   }
}

So far, I can think of one workaround, storing an additional Object (basically an array with strings as keys) for quick retrieval. This works since I know the “prefix” value will not change.

var userArray:Array = new Array();
var prefixArray:Object = {};
function addUser(user:User):void
{
   userArray.push(user);
   prefixArray[user.prefix] = user;
}

function getUserByPrefix(prefix:String):User
{
   return prefixArray[prefix];
}

The problem is, for each UserList instance there may be 4 or 5 arrays with values. If there are 30 UserList instances, that means there are 150 arrays each containing maybe 100 User references in them. Also, this system won’t work in case properties such as “prefix” change over time.

This probably isn’t too bad compared to how bad it could be.

Are there any more ways of doing this efficiently?