English 中文(简体)
Objective-C, sorting an array based on a objects instance variable.
原标题:

I m working on a game like Rodents Revenge, just to point out where I m coming from with this question. I m using the cocos2d game engine aswell...

I have a layer that contains about 168 blocks, these blocks are a subclass of a Sprite. Each block contains two instance variables that are integers, one for the xGridLocation and yGridLocation. I have a method that I call that returns an array containing all the blocks that are on the same x or y row as the main character your controlling (the mouse). This method works as long as the blocks stay in the same order (smallest x/y value to largest), but when I start pushing the blocks out of their original row and mixing them up a bit (when playing the game) my logic no longer works because it is based on the fact that the blocks in the array are indexed from smallest to largest based on their xGridLocation or yGridLocation. The xGridLocation and yGridLocation are not based on their position, when the layer first loads they are given preset grid locations and as the block moves in any direction the grid location is changed based on which direction they moved.

My question is how can I go about sorting the array before it s returned in order based on the instance variables xGridLocation or yGridLocation. I was thinking using the sortUsingSelector:@selector(compareValues:) method but wasn t sure how to go about implementing the compareValues method that will do the sorting.

Here is my method I used for getting an array of blocks.

//I have another one for x. The y parameter is the mouses y value.
-(NSMutableArray *)getBlocksForY:(int)y
{
 NSMutableArray *blocks = [[NSMutableArray alloc] init];

 int tagNum = 0;

        //tagNum starts at 0, and goes up to 168, the numer of children (blocks) on
        //this layer...

 for(tagNum; tagNum<=168; tagNum++)
 {
  BlueBlock *currentBlock = (BlueBlock *)[self getChildByTag:tagNum];
  int currentY = [currentBlock getBlockLocationY];

                //Checks to see if the current block has same y value as the mouse, if
                //so it adds it to the array.
  if(currentY == y)
  {
   [blocks addObject:currentBlock];
  }
 }

        //I want to sort before returning...
 return blocks;
}

If you need more information just ask.

问题回答

As stated in NSMutableArray reference:

The comparator message is sent to each object in the receiver and has as its single argument another object in the array. The comparator method should return NSOrderedAscending if the receiver is smaller than the argument, NSOrderedDescending if the receiver is larger than the argument, and NSOrderedSame if they are equal.

So your comparator should be something like this, and should be added to BlueBlock class:

- (NSInteger) compareBlocks:(BlueBlock)block
{
     if ([self getBlockLocationX] < [block getBlockLocationX])
          return NSOrderedAscending;
     else if ([self getBlockLocationX] == [block getBlockLocationX])
     {
          if ([self getBlockLocationY] < [block getBlockLocationY])
              return NSOrderedAscending;
          else if ([self getBlockLocationY] == [block getBlockLocationY])
              return NSOrderedSame;
          else
              return NSOrderedDescending;
     }
     else
         return NSOrderedDescending;
 }




相关问题
How do I sort enum members alphabetically in Java?

I have an enum class like the following: public enum Letter { OMEGA_LETTER("Omega"), GAMMA_LETTER("Gamma"), BETA_LETTER("Beta"), ALPHA_LETTER("Alpha"), private final String ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

Can I Nest OrderBy in .NET?

This doesn t seem to work as I intend. VB.NET: Dim x = Model.Discussions.OrderByDescending(Function(d) d.Messages.OrderByDescending(Function(m) m.Sent).First.Sent) For Each d As Discussion In x ....

sorting elements javascript

I m looking for a way to sort my elements, but it isn t as easy as it sounds. Please let me explain My elements are grouped per 6 elements (thumbnails), each x represents a thumbnail However all ...

热门标签