• <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://www.shnenglu.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
            posted on 2011-12-02 00:03 逛奔的蝸牛 閱讀(434) 評論(0)  編輯 收藏 引用 所屬分類: Cocoa
            蜜臀av性久久久久蜜臀aⅴ | 久久影院综合精品| 人妻无码αv中文字幕久久| 久久国产免费观看精品| 免费一级欧美大片久久网 | 亚洲国产日韩欧美久久| 久久九九兔免费精品6| 精品亚洲综合久久中文字幕| 91精品国产91久久| 久久久精品2019免费观看| 久久热这里只有精品在线观看| 国内精品久久久久影院免费| 久久久久久久精品成人热色戒| 一级女性全黄久久生活片免费| 国产精品免费看久久久| 无码8090精品久久一区| 久久精品国产亚洲AV无码麻豆| 久久久国产精华液| 97久久精品无码一区二区天美| 亚洲精品乱码久久久久久蜜桃| 国产亚洲婷婷香蕉久久精品| 久久婷婷国产剧情内射白浆| 久久国产精品一区| 国产福利电影一区二区三区久久久久成人精品综合 | 久久国产色av免费看| 欧美国产成人久久精品| 亚洲色欲久久久综合网东京热| 亚洲国产欧美国产综合久久| 高清免费久久午夜精品| 无码任你躁久久久久久| 久久综合综合久久综合| 久久99中文字幕久久| 国产偷久久久精品专区| 亚洲国产精品久久66| 亚洲中文字幕久久精品无码喷水 | 久久天堂电影网| 高清免费久久午夜精品| 精品久久久久久国产免费了| 久久99国产精品二区不卡| 中文字幕乱码久久午夜| 无码国内精品久久人妻麻豆按摩|