• <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 逛奔的蝸牛 閱讀(427) 評論(0)  編輯 收藏 引用 所屬分類: Cocoa
            久久久久久a亚洲欧洲aⅴ| 一本久久免费视频| 久久综合亚洲欧美成人| 精品久久久久久无码专区 | 久久中文字幕一区二区| 国产精品欧美久久久久无广告| 久久综合久久美利坚合众国| 69国产成人综合久久精品| 亚洲精品乱码久久久久久蜜桃| 中文字幕久久精品无码| 天天综合久久一二三区| 成人综合伊人五月婷久久| 久久中文字幕视频、最近更新 | 久久综合久久自在自线精品自| 久久国产精品-国产精品| 久久一日本道色综合久久| 狠狠色伊人久久精品综合网 | 亚洲国产精品无码久久| 国产精品美女久久福利网站| 青青草原综合久久大伊人精品| 久久久久久久久久久精品尤物| 日本久久中文字幕| 亚洲国产成人久久精品影视| 免费观看久久精彩视频| 久久精品国产亚洲av麻豆小说| 国色天香久久久久久久小说| 久久精品无码一区二区三区日韩| 无夜精品久久久久久| 久久播电影网| 26uuu久久五月天| 久久久久综合网久久| 久久超碰97人人做人人爱| 亚洲AV无一区二区三区久久 | 亚洲精品无码成人片久久| 国产99久久久国产精品小说| 深夜久久AAAAA级毛片免费看| 久久精品国产只有精品66| 久久er国产精品免费观看8| 精品久久久久久国产三级| 久久久久久国产精品免费免费| 国产亚州精品女人久久久久久 |