How do I sort an NSMutableArray with custom objects in it?

前端 未结 27 3261
予麋鹿
予麋鹿 2020-11-21 04:45

What I want to do seems pretty simple, but I can\'t find any answers on the web. I have an NSMutableArray of objects, and let\'s say they are \'Person\' objects

27条回答
  •  半阙折子戏
    2020-11-21 05:10

    NSMutableArray *stockHoldingCompanies = [NSMutableArray arrayWithObjects:fortune1stock,fortune2stock,fortune3stock,fortune4stock,fortune5stock,fortune6stock , nil];
    
    NSSortDescriptor *sortOrder = [NSSortDescriptor sortDescriptorWithKey:@"companyName" ascending:NO];
    
    [stockHoldingCompanies sortUsingDescriptors:[NSArray arrayWithObject:sortOrder]];
    
    NSEnumerator *enumerator = [stockHoldingCompanies objectEnumerator];
    
    ForeignStockHolding *stockHoldingCompany;
    
    NSLog(@"Fortune 6 companies sorted by Company Name");
    
        while (stockHoldingCompany = [enumerator nextObject]) {
            NSLog(@"===============================");
            NSLog(@"CompanyName:%@",stockHoldingCompany.companyName);
            NSLog(@"Purchase Share Price:%.2f",stockHoldingCompany.purchaseSharePrice);
            NSLog(@"Current Share Price: %.2f",stockHoldingCompany.currentSharePrice);
            NSLog(@"Number of Shares: %i",stockHoldingCompany.numberOfShares);
            NSLog(@"Cost in Dollars: %.2f",[stockHoldingCompany costInDollars]);
            NSLog(@"Value in Dollars : %.2f",[stockHoldingCompany valueInDollars]);
        }
        NSLog(@"===============================");
    

提交回复
热议问题