• <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 逛奔的蝸牛 閱讀(426) 評論(0)  編輯 收藏 引用 所屬分類: Cocoa
            99久久精品国内| 久久亚洲精品国产精品婷婷| 精品国产乱码久久久久久呢| 久久精品国产99国产精品导航 | 国产无套内射久久久国产| 国产三级精品久久| 伊人久久综合无码成人网| 久久99国产综合精品| 久久综合久久鬼色| 99久久精品国产麻豆| 狠狠色丁香婷婷久久综合| 久久国产乱子精品免费女| 久久综合亚洲色一区二区三区| 狠狠狠色丁香婷婷综合久久俺| 亚洲国产精品嫩草影院久久| 国产精品一区二区久久不卡| 久久成人小视频| 国产毛片久久久久久国产毛片| 久久久久亚洲AV无码网站| 色天使久久综合网天天| 国产AⅤ精品一区二区三区久久| 久久久久亚洲AV片无码下载蜜桃| 伊人丁香狠狠色综合久久| 狼狼综合久久久久综合网| 无码8090精品久久一区| 大香网伊人久久综合网2020| 久久精品国产亚洲AV高清热| 久久久久久国产精品无码下载| 久久久久亚洲AV无码专区桃色| 99久久精品免费| 国内精品久久九九国产精品| 亚洲人成精品久久久久| 亚洲精品99久久久久中文字幕| 99久久伊人精品综合观看| 久久精品国产久精国产| 久久777国产线看观看精品| 99久久人妻无码精品系列| 久久亚洲精品国产精品| 久久综合给合久久狠狠狠97色| 精品久久久久久久无码| 精品一区二区久久|