简体   繁体   English

NSTableColumn错误地对数字排序

[英]NSTableColumn sorts numbers wrongly

This is a NSTableView with IB bindings to a NSArrayController, it displays all values correctly. 这是一个具有IB绑定到NSArrayController的NSTableView,它正确显示了所有值。
However it sorts the numbers only by their first char value eg it will put 115.31 below 2.5, and 23.9 below 4.71, etc. 但是,它仅按数字的第一个char值对其进行排序,例如,它将使115.31低于2.5,而23.9低于4.71,依此类推。

It takes values from a retained NSMutableArray with Strings in it, I also tried by converting all strings to NSNumber/NSDecimalNumber, still no luck: 它从保留的NSMutableArray中获取带有字符串的值,我还尝试了将所有字符串转换为NSNumber / NSDecimalNumber的方法,但还是没有运气:

NSMutableArray *array1 = [[string1 componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet]] mutableCopy];

NSMutableArray *array1alt = [NSMutableArray array];

for(NSString *strNum in array1)
{
    NSNumber *number = strNum;
    [array1alt addObject:number];
}

Please help, thanks. 请帮忙,谢谢。

EDIT: This is how NSMutableArray(s) of my NSTableColumn(s) get filled: 编辑:这就是我的NSTableColumn的NSMutableArray(s)的填充方式:

NSMutableArray *rows = [NSMutableArray array];

for (NSUInteger i = 0; i < array1alt.count && i < array2.count && i < array3.count && i < array4.count; i++)
{
    NSMutableDictionary *row = [NSMutableDictionary dictionary];
    [row setObject:[array1alt objectAtIndex:i] forKey:@"Apples"];
    [row setObject:[array2 objectAtIndex:i] forKey:@"Oranges"];
    [row setObject:[array3 objectAtIndex:i] forKey:@"Peaches"];
    [row setObject:[array4 objectAtIndex:i] forKey:@"Plums"];
    [rows addObject:row];
}

[myArrayController2 setContent:rows2];
[aTableView reloadData];

I'm surprised that you aren't getting a compiler warning at: 我很惊讶您在以下位置没有收到编译器警告:

NSNumber *number = strNum;

You probably want: 您可能想要:

NSNumber *number = [NSNumber numberWithDouble:[strNum doubleValue]];

Or, more simply: 或者,更简单地说:

NSNumber *number = @([strNum doubleValue]);

If you don't want to deal with number conversions on the output, you could sort your original array of strings like so: 如果您不想在输出中处理数字转换,则可以对原始字符串数组进行排序,如下所示:

NSArray *array2 = [array1 sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {
    double num1 = [obj1 doubleValue];
    double num2 = [obj2 doubleValue];
    if (num1 < num2)
        return NSOrderedAscending;
    else if (num1 > num2)
        return NSOrderedDescending;
    else
        return NSOrderedSame;
}];

If you want to use decimal numbers, you could probably do something like: 如果要使用十进制数字,则可以执行以下操作:

NSMutableArray *array2 = [NSMutableArray array];
for (NSString *strNum in array1)
{
    [array2 addObject:[NSDecimalNumber decimalNumberWithString:strNum]];
}

[array2 sortUsingComparator:^NSComparisonResult(NSDecimalNumber *obj1, NSDecimalNumber *obj2) {
    return [obj1 compare:obj2];
}];

Without seeing more code this is what i think: 没有看到更多的代码,这就是我的想法:

If i understood correctly, you have your NSArrayController as data source. 如果我正确理解,则可以将NSArrayController用作数据源。

You need to sort your data before attaching it to your table. 您需要先对数据进行排序,然后再将其附加到表中。

You have NSArrayController methods: 您有NSArrayController方法:

- (void)setSortDescriptors:(NSArray *)sortDescriptors

and

- (NSArray *)arrangeObjects:(NSArray *)objects

With this you will get sorted array to use for your table. 这样,您将获得用于表的排序数组。 Maybe you will need to call reloadData of your NSTableView. 也许您需要调用NSTableView的reloadData

I cant test these right now because i'm at my laptop right now which doesn't have MacOS :) 我现在无法测试这些内容,因为我现在正在使用没有MacOS的笔记本电脑:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM