• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            逛奔的蝸牛

            我不聰明,但我會很努力

               ::  :: 新隨筆 ::  ::  :: 管理 ::
            Question: What I want to do seems pretty simple, but I can't find any answers on the web. I have an NSMutableArray of objects, let's say they are 'Person' objects. I want to sort the NSMutable array by Person.birthDate which is an NSDate.

            1. Either you implement a compare-method for your object:

            - (NSComparisonResult)compare:(Person *)otherObject {
               
            return [self.birthDate compare:otherObject.birthDate];
            }

            NSArray *sortedArray = [drinkDetails sortedArrayUsingSelector:@selector(compare:)];


            2. or usually even better: (The default sorting selector of NSSortDescriptor is compare:)

            NSSortDescriptor *sortDescriptor;
            sortDescriptor
            = [[[NSSortDescriptor alloc] initWithKey:@"birthDate"
                                                          ascending
            :YES] autorelease];
            NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
            NSArray *sortedArray;
            sortedArray
            = [drinkDetails sortedArrayUsingDescriptors:sortDescriptors];

            3. See the NSMutableArray method sortUsingFunction:context:
            NSComparisonResult compare(Person *firstPerson, Person *secondPerson, void *context) {
             
            if ([firstPerson birthDate] < [secondPerson birthDate])
               
            return NSOrderedAscending;
             
            else if ([firstPerson birthDate] > [secondPerson birthDate])
               
            return NSOrderedDescending;
             
            else
               
            return NSOrderedSame;
            }
            int SortPlays(id a,  id b, void* context) {
               
            Play* p1=a;
               
            Play* p2=b;
               
            if (p1.score<p2.score) return NSOrderedDescending;
               
            else if (p1.score>p2.score) return NSOrderedAscending;
               
            return NSOrderedSame;
            }

            ...
            [validPlays sortUsingFunction:SortPlays context:nil];

            4. Starting in iOS 4 you can also use blocks for sorting.
            NSArray *arrayToSort = where ever you get the array from... ;
            NSComparisonResult (^sortBlock)(id, id) = ^(id obj1, id obj2) {
             
            if ([obj1 position] > [obj2 position]) {
               
            return (NSComparisonResult)NSOrderedDescending;
             
            }
             
            if ([obj1 position] < [obj2 position]) {
               
            return (NSComparisonResult)NSOrderedAscending;
             
            }
             
            return (NSComparisonResult)NSOrderedSame;
            };
            NSArray *sorted = [arrayToSort sortedArrayUsingComparator:sortBlock];
            featuresArray = [unsortedFeaturesArray sortedArrayUsingComparator: ^(id a, id b){ ... }]
            @import url(http://m.shnenglu.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
            posted on 2011-12-02 00:03 逛奔的蝸牛 閱讀(426) 評論(0)  編輯 收藏 引用 所屬分類: Cocoa
            欧美亚洲国产精品久久高清| A级毛片无码久久精品免费| 精品久久久久久无码国产| 人人狠狠综合久久亚洲88| 久久综合九色综合精品| 久久人妻少妇嫩草AV无码蜜桃| 久久99精品国产99久久6| 香蕉久久久久久狠狠色| 国产Av激情久久无码天堂| 91久久精品无码一区二区毛片| 亚洲AⅤ优女AV综合久久久| 亚洲午夜久久久影院伊人| 色综合久久中文综合网| 综合久久国产九一剧情麻豆| 91精品国产91久久久久久青草| 久久青青色综合| 国产午夜电影久久| 久久精品国产亚洲精品2020| 久久综合九色综合欧美就去吻| 亚洲国产另类久久久精品黑人| 久久久久国产精品嫩草影院| 久久国产亚洲精品无码| 久久亚洲国产成人影院| 无码任你躁久久久久久| 久久综合狠狠综合久久激情 | 欧美喷潮久久久XXXXx| 久久精品一区二区三区AV| 久久久受www免费人成| 99久久国产主播综合精品| 久久精品一本到99热免费| 久久久久久国产精品无码下载| 久久久艹| 久久综合九色综合久99| 亚洲欧洲精品成人久久曰影片| 国产精品欧美久久久久天天影视 | 精品久久久久久久国产潘金莲| 久久香蕉国产线看观看乱码| 99久久精品影院老鸭窝| 亚洲国产精品一区二区久久| 久久综合欧美成人| 久久久中文字幕日本|