ios - 排序描述符在 ios 中不起作用

标签 ios sorting nssortdescriptor

我使用排序描述符按一个和多个值对 NSMutableArray 进行排序,首先我尝试按价格排序,它按其他顺序排序,这是我的代码帮助我, 我的 我通过下面的代码创建字典并添加到 NSMutableArray

    for(int i=0;i<[priceArray count];i++)
    {
        cellDict=[[NSMutableDictionary alloc]init];
        [cellDict setObject:nameArray[i] forKey:@"Name"];
        [cellDict setObject:splPriceArray[i] forKey:@"Percentage"];
        [cellDict setObject:priceArray[i] forKey:@"Price"];
        [resultArray addObject:cellDict];
    }
// To Sort in Ascending Order
    NSSortDescriptor *sort =[[NSSortDescriptor alloc] initWithKey:@"Price" ascending:YES];
    NSArray *descriptors = [NSArray arrayWithObjects:sort, nil];
    NSArray *sortedArray=[resultArray sortedArrayUsingDescriptors:descriptors];

    NSLog(@"Result %@ Sorted arr %@",resultArray, sortedArray);

输出是:

 Result (
        {
        Name = "Black Eyed Peas";
        Percentage = 0;
        Price = 80;
    },
        {
        Name = "Black Gram";
        Percentage = 0;
        Price = 56;
    },
        {
        Name = "Channa White";
        Percentage = 0;
        Price = 100;
    },
        {
        Name = "Double Beans";
        Percentage = 0;
        Price = 95;
    },
        {
        Name = "Gram Dall";
        Percentage = 0;
        Price = 100;
    },
        {
        Name = "Green Moong Dal";
        Percentage = 0;
        Price = 150;
    },
        {
        Name = "Ground Nut";
        Percentage = 0;
        Price = 140;
    },
        {
        Name = "Moong Dal";
        Percentage = 0;
        Price = 75;
    },
        {
        Name = "Orid Dal";
        Percentage = 0;
        Price = 100;
    },
        {
        Name = "Toor Dal";
        Percentage = 0;
        Price = 150;
    }
) Sorted arr (
        {
        Name = "Channa White";
        Percentage = 0;
        Price = 100;
    },
        {
        Name = "Gram Dall";
        Percentage = 0;
        Price = 100;
    },
        {
        Name = "Orid Dal";
        Percentage = 0;
        Price = 100;
    },
        {
        Name = "Ground Nut";
        Percentage = 0;
        Price = 140;
    },
        {
        Name = "Green Moong Dal";
        Percentage = 0;
        Price = 150;
    },
        {
        Name = "Toor Dal";
        Percentage = 0;
        Price = 150;
    },
        {
        Name = "Black Gram";
        Percentage = 0;
        Price = 56;
    },
        {
        Name = "Moong Dal";
        Percentage = 0;
        Price = 75;
    },
        {
        Name = "Black Eyed Peas";
        Percentage = 0;
        Price = 80;
    },
        {
        Name = "Double Beans";
        Percentage = 0;
        Price = 95;
    }
)

这里是按其他顺序排序的已排序数组,我想按价格按升序对其进行排序。

最佳答案

目前还不清楚您的测试数据是什么样的 - 但以下代码片段按预期工作

NSArray *priceArray = [NSArray arrayWithObjects:@(74),@(100),@(100),@(130), nil];
    NSArray *nameArray = [NSArray arrayWithObjects:@"Yva",@"Hallo", @"Adam", @"Xavier", nil];

NSMutableArray *resultArray = [NSMutableArray new];
for(int i=0;i<[priceArray count];i++)
{
    NSMutableDictionary *cellDict=[[NSMutableDictionary alloc]init];
    [cellDict setObject:nameArray[i] forKey:@"Name"];
    [cellDict setObject:priceArray[i] forKey:@"Percentage"];
    [cellDict setObject:priceArray[i] forKey:@"Price"];
    [resultArray addObject:cellDict];
}
// Sort by Name
//NSSortDescriptor *sort =[[NSSortDescriptor alloc] initWithKey:@"Price" ascending:YES];

// Sort by Name
NSSortDescriptor *sort =[[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];

NSArray *descriptors = [NSArray arrayWithObjects:sort, nil];
NSArray *sortedArray=[resultArray sortedArrayUsingDescriptors:descriptors];

NSLog(@"Result %@ Sorted arr %@",resultArray, sortedArray);

结果:

2015-07-11 12:54:54.358 ret[10480:162783] Result (
        {
        Name = Yva;
        Percentage = 74;
        Price = 74;
    },
        {
        Name = Hallo;
        Percentage = 100;
        Price = 100;
    },
        {
        Name = Adam;
        Percentage = 100;
        Price = 100;
    },
        {
        Name = Xavier;
        Percentage = 130;
        Price = 130;
    }
) Sorted arr (
        {
        Name = Adam;
        Percentage = 100;
        Price = 100;
    },
        {
        Name = Hallo;
        Percentage = 100;
        Price = 100;
    },
        {
        Name = Xavier;
        Percentage = 130;
        Price = 130;
    },
        {
        Name = Yva;
        Percentage = 74;
        Price = 74;
    }
)

关于ios - 排序描述符在 ios 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31355418/

相关文章:

ios - 将 subview 添加到 UICollectionViewCell 一次(单元格可用性问题)

ios - 包含在对 Firebase 的查询中

sorting - 按多个键对 Racket 中的结构列表进行排序

java - 在 Java 集合排序中使用 Comparator 接口(interface)比较方法返回值 -1, 0, 1 到底意味着什么?

ios - 是否无法使用 NSSortDescriptor 按 BOOL 属性排序?

objective-c - 使用类别和核心数据进行自定义排序 - 框架是否支持?

ios - 根据关系属性对获取请求进行排序

ios - NSForegroundColorAttributeName 不适用于 UILabel

mysql - 按 Soundex(或类似)排序 `Closeness`

ios - 在后台录制 IOS 模拟器视频和模拟器音频