对 NSDictionary 进行排序(其中包含我的 Facebook 好友列表)

发布于 2024-11-26 23:01:39 字数 855 浏览 0 评论 0原文

通过此代码使用 Graph Facebook API

[facebook requestWithGraphPath:@"me/friends" andDelegate:self]; 

现在我有一个 NSDictionary,如下所示:

{data =     (
            {
        id = 123456;
        name = "Mark Zuckerberg";
    },
            {
        id = 654321;
        name = "Steve Jobs";
    },
            {
        id = 13579;
        name = "Bill Gates";
    }
    ...
  );
}

我想对此 NSDictionary 进行排序,但我知道这是不可能的。 当然,我可以创建2个NSArray(或NSMutableArray),一个“array_id”,另一个“array_name”。 我想对“array_name”进行排序,但保留对 id 的引用。

例如,如果我的两个数组是这样的:

array_name = {"Mark", "Steve", "Bill};
array_id   = {"123456", "654321", "13579"};

最后我想要这样的东西:

array_name = {"Bill", "Mark", "Steve"};
array_id   = {"13579", "123456", "654321"};

你对我有什么建议?谢谢!

Using the Graph Facebook API with this code

[facebook requestWithGraphPath:@"me/friends" andDelegate:self]; 

Now I have an NSDictionary which is like this:

{data =     (
            {
        id = 123456;
        name = "Mark Zuckerberg";
    },
            {
        id = 654321;
        name = "Steve Jobs";
    },
            {
        id = 13579;
        name = "Bill Gates";
    }
    ...
  );
}

I want to sort this NSDictionary, but I know that is not possible.
Of course, I can create 2 NSArray (or NSMutableArray), one "array_id" and the other one "array_name".
I'd like to sort "array_name", but keeping the reference to the id.

For example, if my two arrays are something like this:

array_name = {"Mark", "Steve", "Bill};
array_id   = {"123456", "654321", "13579"};

I want, at the end, something like this:

array_name = {"Bill", "Mark", "Steve"};
array_id   = {"13579", "123456", "654321"};

What do you suggest me? Thanks!

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

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

发布评论

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

评论(4

看起来您不想对 NSDictionary 进行排序,而是想对 NSDictionary 对象的 NSArray 进行排序,而该对象本身恰好是在 NSDictionary 中。您可以简单地提取这个朋友数组,如下所示:

NSArray *unsortedFriends = [dictionaryFromFacebook objectForKey:@"data"];

现在您可以执行以下操作:

NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
NSArray *sortedFriends = [unsortedFriends sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];

It doesn't look like you want to sort an NSDictionary, you want to sort an NSArray of NSDictionary objects that just so happens to itself be in an NSDictionary. You can simply extract this friends array as follows:

NSArray *unsortedFriends = [dictionaryFromFacebook objectForKey:@"data"];

Now you can do the following:

NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
NSArray *sortedFriends = [unsortedFriends sortedArrayUsingDescriptors:[NSArray arrayWithObject:descriptor]];
各自安好 2024-12-03 23:01:39

您可以使用 NSArray 的 sortedArrayUsingComparator: 方法来完成此操作。比较器块将返回比较其参数名称的结果。

NSDictionary *requestResult = ...; // get your original dictionary
NSArray *unsortedArray = [requestResult objectForKey:@"data"];
NSArray *sortedArray = [unsortedArray sortedArrayUsingComparator:^(id obj1, id obj2) {
    NSString *name1 = (NSString *)[(NSDictionary *)obj1 objectForKey:@"name"];
    NSString *name2 = (NSString *)[(NSDictionary *)obj2 objectForKey:@"name"];
    return [name1 caseInsensitiveCompare:name2];
}];

You can do this using the sortedArrayUsingComparator: method of NSArray. The comparator block would return the result of comparing the names of its arguments.

NSDictionary *requestResult = ...; // get your original dictionary
NSArray *unsortedArray = [requestResult objectForKey:@"data"];
NSArray *sortedArray = [unsortedArray sortedArrayUsingComparator:^(id obj1, id obj2) {
    NSString *name1 = (NSString *)[(NSDictionary *)obj1 objectForKey:@"name"];
    NSString *name2 = (NSString *)[(NSDictionary *)obj2 objectForKey:@"name"];
    return [name1 caseInsensitiveCompare:name2];
}];
誰ツ都不明白 2024-12-03 23:01:39

简单的事情就是创建一种新的对象:

@interface Friend : NSObject

@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *facebookID;

@end

@implementation Friend

@synthesize name, facebookID;

- (void)dealloc {
  [name release];
  [facebookID release];
  [super dealloc];
}

@end

当你有了这个对象时,你可以循环遍历你返回的数组并执行以下操作:

NSMutableArray *friends = [NSMutableArray array];
for (NSDictionary *rawFriend in arrayOfFriends) {
  NSString *name = [rawFriend objectForKey:@"name"];
  NSString *fID = [rawFriend objectForKey:@"id"];

  if (name && fID) {
    Friend *f = [[Friend alloc] init];
    [f setName:name];
    [f setFacebookID:fID];
    [friends addObject:f];
    [f release];
  }
}

完成后,对事物进行排序就很简单:

NSSortDescriptor *sortByName = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
[friends sortUsingDescriptors:[NSArray arrayWithObject:sortByName]];

The simple thing would be to create a new kind of object:

@interface Friend : NSObject

@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *facebookID;

@end

@implementation Friend

@synthesize name, facebookID;

- (void)dealloc {
  [name release];
  [facebookID release];
  [super dealloc];
}

@end

When you have this, you can loop through your array that you got back and do:

NSMutableArray *friends = [NSMutableArray array];
for (NSDictionary *rawFriend in arrayOfFriends) {
  NSString *name = [rawFriend objectForKey:@"name"];
  NSString *fID = [rawFriend objectForKey:@"id"];

  if (name && fID) {
    Friend *f = [[Friend alloc] init];
    [f setName:name];
    [f setFacebookID:fID];
    [friends addObject:f];
    [f release];
  }
}

Once you've done that, it's simple to sort things:

NSSortDescriptor *sortByName = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
[friends sortUsingDescriptors:[NSArray arrayWithObject:sortByName]];
红衣飘飘貌似仙 2024-12-03 23:01:39

您可以使用 3 种方法中的任何一种 -keysSortedByValueUsingSelector:, -keysSortedByValueUsingComparator:,或-keysSortedByValueWithOptions:usingComparator: 获取按顺序排序的键数组。因此,如果您想按顺序迭代,可以执行以下操作:

NSDictionary *myDict = ...;
for (id key in [myDict keysSortedByValueUsingSelector:@selector(compare:)])
{
    id value = [myDict objectForKey:key];
    // Do stuff with (key, value)
}

如果您需要一个单独的数组,其中包含与键的排序顺序相对应的顺序的所有值,则执行上述迭代并构造该数组。

You can use any of the 3 methods -keysSortedByValueUsingSelector:, -keysSortedByValueUsingComparator:, or -keysSortedByValueWithOptions:usingComparator: to get an array of the keys sorted in order. So, if you want to iterate in order, you can do something like this:

NSDictionary *myDict = ...;
for (id key in [myDict keysSortedByValueUsingSelector:@selector(compare:)])
{
    id value = [myDict objectForKey:key];
    // Do stuff with (key, value)
}

If you need a separate array containing all of the values in the order corresponding to the sorted order of the keys, then do the above iteration and construct that array.

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