• <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
            久久精品无码一区二区无码| 久久久精品午夜免费不卡| 欧美国产成人久久精品| 亚洲国产视频久久| 亚洲精品无码久久久久sm| 国产成人精品久久二区二区| 国内精品久久久久影院免费| 日韩欧美亚洲综合久久影院Ds | 久久久久99精品成人片欧美| 成人资源影音先锋久久资源网| 国産精品久久久久久久| 久久久久99这里有精品10 | 亚洲精品国产美女久久久| 久久免费美女视频| 国产亚洲精久久久久久无码AV| 狠狠综合久久综合88亚洲| 国内精品久久久久国产盗摄| av色综合久久天堂av色综合在| 婷婷综合久久狠狠色99h| 伊人久久大香线蕉综合Av| 久久99精品国产麻豆婷婷| 99久久国语露脸精品国产| 久久久噜噜噜久久中文字幕色伊伊 | 国产免费久久精品99久久| 7777精品久久久大香线蕉| 一级a性色生活片久久无| 91精品国产91久久| 久久久91精品国产一区二区三区 | 久久无码中文字幕东京热| 青青青青久久精品国产h| 久久精品国产亚洲av高清漫画| 亚洲精品99久久久久中文字幕| AA级片免费看视频久久| 久久国产精品成人片免费| 久久这里只有精品18| 日本人妻丰满熟妇久久久久久| 无码人妻久久一区二区三区蜜桃 | 伊人久久大香线蕉av一区| 欧美日韩精品久久久久| 久久亚洲精品无码观看不卡| 欧美亚洲日本久久精品|