在 Objective-C iPhone 中按数组中的众数排序

发布于 2024-08-26 07:08:57 字数 220 浏览 10 评论 0原文

如果我有一个 NSArray,其中包含一些值。有没有一种方法使用描述符将其按数组中最常见的数字首先排序,最后按最不常见的数字排序,

Array has(
"3",
"2",
"1",
"3",
"3",
"7",
)

Array has(
"3",
"3",
"3",
"1",
"2",
"7",
)

If I have an NSArray with some values in them. Is there a way using descriptors to sort it by the most frequent number in the array first and the least frequent number at the end,

Array has(
"3",
"2",
"1",
"3",
"3",
"7",
)

to

Array has(
"3",
"3",
"3",
"1",
"2",
"7",
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给不了的爱 2024-09-02 07:08:57
@interface NSArray (Ext)
-(NSArray*) sortByMostFrequent ;
@end



@implementation NSArray (Ext)
-(NSArray*) sortByMostFrequent {
    NSMutableDictionary* frequencyDict = [NSMutableDictionary dictionary];
    for (id obj in self) {
        int frequency = [[frequencyDict valueForKey:obj] intValue];
        [frequencyDict setValue:[NSNumber numberWithInt:frequency+1] forKey:obj];
    }
    NSMutableArray* ary = [NSMutableArray arrayWithCapacity:self.count];
    for (id obj in self) {
        [ary addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                        obj, @"Object",
                        [frequencyDict valueForKey:obj], @"Frequency",
                        nil]];
    }
    NSSortDescriptor* sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Frequency" ascending:NO];
    [ary sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    [sortDescriptor release];
    return [ary valueForKey:@"Object"];
}
@end



/// example
NSArray* ary = [NSArray arrayWithObjects:@"3", @"2", @"1", @"3", @"3", @"7", nil];
NSLog(@"ary %@", [ary sortByMostFrequent]);
@interface NSArray (Ext)
-(NSArray*) sortByMostFrequent ;
@end



@implementation NSArray (Ext)
-(NSArray*) sortByMostFrequent {
    NSMutableDictionary* frequencyDict = [NSMutableDictionary dictionary];
    for (id obj in self) {
        int frequency = [[frequencyDict valueForKey:obj] intValue];
        [frequencyDict setValue:[NSNumber numberWithInt:frequency+1] forKey:obj];
    }
    NSMutableArray* ary = [NSMutableArray arrayWithCapacity:self.count];
    for (id obj in self) {
        [ary addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                        obj, @"Object",
                        [frequencyDict valueForKey:obj], @"Frequency",
                        nil]];
    }
    NSSortDescriptor* sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Frequency" ascending:NO];
    [ary sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    [sortDescriptor release];
    return [ary valueForKey:@"Object"];
}
@end



/// example
NSArray* ary = [NSArray arrayWithObjects:@"3", @"2", @"1", @"3", @"3", @"7", nil];
NSLog(@"ary %@", [ary sortByMostFrequent]);
聆听风音 2024-09-02 07:08:57

并非没有一些额外的数据结构。

所有 NSArray 排序操作(sortUsingDescriptors:、sortedArrayUsingSelector: 等)都假设您可以查看两个元素“a”和“b”并确定是否“a < b”,而无需查看 NSArray 中的任何其他元素。

一种解决方案是创建一个新数组,其成员对象同时包含值和频率计数(使用 NSDictionary 有效计算每个值有多少行)。例如:

Array( // {value, frequency}
{3,3},
{2,1},
{1,1},
{3,3},
{3,3},
{7,1}
)

然后很容易使用描述符按频率对该数组进行排序。

Not without some additional data structures.

All the NSArray sorting operations (sortUsingDescriptors:, sortedArrayUsingSelector:, etc.) assume that you can look at two elements "a" and "b" and determine if "a < b" without looking at any other elements in the NSArray.

One solution would be to create a new Array whose member objects contain both the value and the frequency count (use an NSDictionary to efficiently count how many rows there are for each value). For example:

Array( // {value, frequency}
{3,3},
{2,1},
{1,1},
{3,3},
{3,3},
{7,1}
)

Then it's easy to use a descriptor to sort that array by frequency.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文