-1

Initially I have shuffled the array elements. Now how do I sort these array elements in a definite order. This is for the purpose of a card game in iOS.

halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

1

You can sort an array using sortedArrayUsingComparator:

[cards sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
    Card *c1 = (Card *)obj1;
    Card *c2 = (Card *)obj2;

    if (c1.value == c2.value) return NSOrderedSame;
    return (c1.value > c2.value) ? NSOrderedDescending : NSOrderedAscending;
}];
neilco
  • 7,964
  • 2
  • 36
  • 41
  • This does not look correct. Your comparator does not return `NSOrderedAscending (-1)` if "c1 < c2". – Martin R Dec 03 '13 at 09:18
  • Cheers @MartinR. Still half asleep this morning. Updated the answer. – neilco Dec 03 '13 at 09:23
  • @neilco i will give an idea of how my array is. there is a main array holding 52 cards. then i created four subarrays as i have four players.before distributing cards to each player main array is shuffled – user2963228 Dec 03 '13 at 09:42
  • 1
    @user2963228 Pertinent information like this should _always_ be included in the original question. What do you want sorting—the main array or the four player arrays? – neilco Dec 03 '13 at 09:47
  • @neilco four players array – user2963228 Dec 03 '13 at 09:49
  • sorry that i didn't include these in question. – user2963228 Dec 03 '13 at 09:49
  • Use the code above on each of the player arrays. – neilco Dec 03 '13 at 09:50
  • @neilco [cards sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) here i hope cards that you meant is array and the Cards is the class name – user2963228 Dec 03 '13 at 09:59
  • `cards` is plural and refers to an array. `Card` is singular and refers to a class that describes the properties of a playing card, including its suit and face value. – neilco Dec 03 '13 at 10:03
1

You have couple of options, you can check NSArray documentation here and look under 'Sorting'.

For quick info, you can use NSSortDescriptors

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"distance" ascending:YES];
NSArray *sortedArray = [shuffledArray sortedArrayUsingDescriptors:@[sortDescriptor]];

they are easy to use, and you can add multiple sort descriptors. You can also use comparators

[sortedArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    // Do your check here and return values below
    // NSOrderedSame
    // NSOrderedDescending
    // NSOrderedAscending
}];

Edit:

Okay, from what I understand from comments below is that you an array with cards that is shuffled initially.

NSArray *shuffledCards

I guess you have Card objects inside that array. If you don't, i think you should. And then there are four players. And again I believe you have Player objects.

For the sake of example:

@interface Card : NSObject
    @property (nonatomic) NSInteger cardNumber;
@end

@interface Player : NSObject
    @property (nonatomic) NSArray *dealtCards;
@end

Let's say you select 10 random cards from the shuffled array and dealt them to each player.

NSArray *randomTenCards = // You get 10 cards somehow

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"cardNumber" ascending:YES];
NSArray * sortedCards = [randomTenCards sortedArrayUsingDescriptors:@[sortDescriptor]];

// The Card objects are now sorted inside sortedCards array according to their cardNumbers.

[self.player1 setDealtCards:sortedCards];
...
...

The basic idea is like that. You can adjust this to your own problem i hope.

mrt
  • 623
  • 4
  • 8
  • I have tried this nssortdescriptor but my concern is it only possible only with array with dictionaries? – user2963228 Dec 03 '13 at 09:16
  • what would i give in the place of sortDescriptorWithKey if using arrays of arrays – user2963228 Dec 03 '13 at 09:19
  • You can use sort descriptors with custom objects as well. It doesn't have to be array of dictionaries. You just give the objects property name as the key parameter. If you have complex structures inside your array, you may be better of using comparator. Since i haven't seen your array or objects, I can't give a clear suggestion on that. – mrt Dec 03 '13 at 09:28
  • i will give an idea of how my array is. there is a main array holding 52 cards. then i created four subarrays as i have four players.before distributing cards to each player main array is shuffled. @mrt – user2963228 Dec 03 '13 at 09:32
  • 3
    @user2963228: It would be more helpful if you add the relevant information to the *question* instead of "hiding" it in the comments. – Martin R Dec 03 '13 at 09:43
  • @MartinR sorry that i didn't include these in question – user2963228 Dec 03 '13 at 09:50
  • and i guess you want the cards inside player arrays to be sorted? Why don't you just sort player arrays individually? Maybe you should edit your question to give more detail, so others can also give their suggestions. – mrt Dec 03 '13 at 09:54
  • @mrt i want to sort each player arrays individually. – user2963228 Dec 03 '13 at 09:55
  • @mrt my class uiviewcontroller not nsobject...is it possible to declare property – user2963228 Dec 03 '13 at 10:21
  • @mrt it is breaking at this NSArray * sortedCards = [User_cards sortedArrayUsingDescriptors:@[sortDescriptor]]; line of code – user2963228 Dec 03 '13 at 10:28
  • @mrt can you please respond to my question – user2963228 Dec 03 '13 at 10:40
  • make sure you are using the correct name as the key of the descriptor. A property with the same name should exists on the object. You should check the class reference https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSSortDescriptor_Class/Reference/Reference.html . If its not a comparable property, there are other options with comparators. Just check the documentations. – mrt Dec 03 '13 at 10:46
  • @mrt is it because my class is not nsobject ? – user2963228 Dec 03 '13 at 10:50
  • @mrt is it because my class is not nsobject ? – user2963228 Dec 03 '13 at 10:57
  • @MartinR is it not possible to use sortedArrayUsingSelector:@selector(compare:) for two-dimensional nsmutable array?? coz when i tried it for single dimensional NSArray it works fine. – user2963228 Dec 03 '13 at 12:31
  • 1
    @user2963228: I can't find the relevant information in your *question* yet: What does your array look like? What kind of objects does it contain? How should the objects be sorted? What result do you expect? What did you try? ..... – Martin R Dec 03 '13 at 12:50
  • @MartinR array here is nsmutable array and it contains image name and card number... this array is shuffled and then passed to each player array.. so it must be like this for e.g. spade1,club5,heart10,diamond2...after sorting it shud look like club5,diamond2,heart10,spade1 – user2963228 Dec 04 '13 at 03:41