Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 目标C:得到错误的输出,我不';为什么我对NSMutableSet进行排序_Objective C_Sorting_Nsmutableset - Fatal编程技术网

Objective c 目标C:得到错误的输出,我不';为什么我对NSMutableSet进行排序

Objective c 目标C:得到错误的输出,我不';为什么我对NSMutableSet进行排序,objective-c,sorting,nsmutableset,Objective C,Sorting,Nsmutableset,我正在对nsmutableSet进行排序,但遇到了一个奇怪的问题 NSArray *data = @[@[@"1",@"2",@"3"], @[@"2",@"3",@"4",@"5",@"6"], @[@"8",@"9",@"10"], @[@"15",@"16",@"17",@"18"]]; NSArray *sortArr = [[NSArray alloc] init]

我正在对nsmutableSet进行排序,但遇到了一个奇怪的问题

   NSArray *data = @[@[@"1",@"2",@"3"],
                   @[@"2",@"3",@"4",@"5",@"6"],
                   @[@"8",@"9",@"10"],
                   @[@"15",@"16",@"17",@"18"]];

NSArray *sortArr = [[NSArray alloc] init];
NSMutableArray *Data = [[NSMutableArray alloc] init];

NSMutableSet *interSection = [[NSMutableSet alloc] init];
interSection = [NSMutableSet setWithArray:[data objectAtIndex:0]];

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"description" ascending:YES ];

for (int i =1; i < 4; i ++) {
    if ([interSection intersectsSet:[NSSet setWithArray:[data objectAtIndex:i]]]) {
        [interSection unionSet:[NSSet setWithArray:[data objectAtIndex:i]]];
    }
    else{

        sortArr = [interSection sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
        [Data addObject:sortArr];
        interSection = [NSMutableSet setWithArray:[data objectAtIndex:i]];

    }
}

if ([interSection count] != 0) {

    sortArr = [interSection sortedArrayUsingDescriptors:[NSArray arrayWithObject:sort]];
    [Data addObject:sortArr];
}
NSLog(@"%@",Data);
NSArray*数据=@[@[@“1”、“2”、“3”],
@[@"2",@"3",@"4",@"5",@"6"],
@[@"8",@"9",@"10"],
@[@"15",@"16",@"17",@"18"]];
NSArray*sortArr=[[NSArray alloc]init];
NSMUTABLEARRY*数据=[[NSMUTABLEARRY alloc]init];
NSMutableSet*交集=[[NSMutableSet alloc]init];
交叉点=[NSMutableSet setWithArray:[data objectAtIndex:0]];
NSSortDescriptor*排序=[NSSortDescriptor SortDescriptor WithKey:@“description”升序:是];
对于(int i=1;i<4;i++){
if([interSection intersectsSet:[NSSet setWithArray:[data objectAtIndex:i]])){
[交叉点联合集:[NSSet setWithArray:[数据对象索引:i]]];
}
否则{
sortArr=[交叉点排序数组使用描述符:[NSArray arrayWithObject:sort]];
[数据添加对象:Sortar];
交叉点=[NSMutableSet setWithArray:[data objectAtIndex:i]];
}
}
如果([交叉点计数]!=0){
sortArr=[交叉点排序数组使用描述符:[NSArray arrayWithObject:sort]];
[数据添加对象:Sortar];
}
NSLog(@“%@”,数据);
但结果是: ( 1. 2. 3. 4. 5. 6. ), ( 10, 8. 9 ), ( 15, 16, 17, 18 ) )

为什么是(10,8,9)但是(8,9,10)


有人知道答案吗?

我想这是因为你使用的是字符串而不是数字。对字符串排序时,10在8之前,因为它以1开头。

您正在对字符串使用
NSSortDescriptor
,因此它以字符串方式排序(8>10,9>10)。您应该创建一个自定义的
NSSortDescriptor
,如下所示:

NSSortDescriptor * sort = [NSSortDescriptor sortDescriptorWithKey:@"sort" ascending:YES comparator:^(id obj1, id obj2) {

    if ([obj1 integerValue] > [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedDescending;
    }
    if ([obj1 integerValue] < [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedAscending;
    }
    return (NSComparisonResult)NSOrderedSame;
}];
sortArr = [[data objectAtIndex:i] sortedArrayUsingDescriptors:[NSArray arrayWithObject: sort]];
NSSortDescriptor*sort=[NSSortDescriptor sortdescriptor with key:@“sort”升序:是比较器:^(id obj1,id obj2){
如果([obj1整型值]>[obj2整型值]){
返回(NSComparisonResult)或撤销;
}
如果([obj1整型值]<[obj2整型值]){
返回(NSComparisonResult)或删除;
}
返回(NSComparisonResult)命令名;
}];
Sortar=[[data objectAtIndex:i]使用描述符进行排序:[NSArray Array WithObject:sort]];

我同意你的想法,将我的数组改为这个NSArray*data=@[@1、@2、@3、@4、@5、@6]、@[@8、@9、@10]、@[@15、@16、@17、@18];输出不正确…谢谢anhtu。你的答案是正确的。顺便说一句,当我试图用data=@[@[@[@1,@2,@3]…]或data=@[@[nsnumber numberwhithint:1],@[nsnumber numberwhithint:2]…]的方式初始化数组时,我仍然找不到正确的anwser。你知道为什么吗?我认为它像对象一样对nsnumber进行排序(可能像字符串一样,我不确定),它不获取NSNumber的intValue。因为它不知道NSNumber是char、float、int还是。。。