如何使用 ios 中的动态键对 NSMutableDictionary 进行排序?
How to sort NSMutableDictionary with dynamic keys in ios?
我正在关注字典作为 JSON 响应
Lease = {
"KINDERSLEY KERROBERT" =
(
{
"lease_code" = 37;
},
{
"lease_code" = 38;
}
);
LLOYDMINSTER =
(
{
"lease_code" = 68;
},
{
"lease_code" = 69;
}
);
"SOUTHEASTERN SASKATCHEWAN" =
(
{
"lease_code" = 1;
},
{
"lease_code" = 2;
}
);
"SWIFT CURRENT" =
(
{
"lease_code" = 32;
},
{
"lease_code" = 33;
}
);
};
我想排序如下:
Lease = {
"SOUTHEASTERN SASKATCHEWAN" =
(
{
"lease_code" = 1;
},
{
"lease_code" = 2;
}
);
"SWIFT CURRENT" =
(
{
"lease_code" = 32;
},
{
"lease_code" = 33;
}
);
"KINDERSLEY KERROBERT" =
(
{
"lease_code" = 37;
},
{
"lease_code" = 38;
}
);
LLOYDMINSTER =
(
{
"lease_code" = 68;
},
{
"lease_code" = 69;
}
);
};
我写了下面的代码来排序字典:
- (NSArray *)sortKeysByIntValue:(NSDictionary *)dictionary {
NSArray *sortedKeys = [dictionary keysSortedByValueUsingComparator:^NSComparisonResult(NSArray *obj1, NSArray *obj2) {
NSString* key1 = [[obj1 objectAtIndex:0] objectForKey:@"lease_code"];
NSString* key2 = [[obj2 objectAtIndex:0] objectForKey:@"lease_code"];
return [key1 compare:key2];
}];
return sortedKeys;
}
它给了我想要的 sortedKeys,但是在使用下面的代码重建 NSMutableDictionary
之后:
NSArray *sortedKeys = [self sortKeysByIntValue:dictionary];
NSMutableDictionary *sortedDictionary = [[NSMutableDictionary alloc] init];
for (NSString *key in sortedKeys) {
[sortedDictionary setObject:dictionary[key] forKey:key];
}
它再次未排序,如响应中所示。
请帮帮我
NSArray *sortedKeys = [self sortKeysByIntValue:dictionary];
NSMutableArray* values = [NSMutableArray array];
for (NSString* key in sortedKeys)
{
[values addObject:dictionary[key]];
}
现在您已经对键和值进行了排序。这正是您所需要的
字典总是未排序的。它们用作键值存储来查找数据。您应该将数据的顺序与字典分开。你需要你已经拥有的字典和一个排序的键数组。
@property NSDictionary *dataDict;
@property NSArray *sortedKeys;
self.sortedKeys = [self sortKeysByIntValue:self.dataDict];
在 UITableViewDataSource
方法中,您将首先使用索引查询数组,获取键,然后从字典中检索数据。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger row = [indexPath row];
NSString *key = [self.sortedkeys objectAtIndex:row];
NSObject *dataObject = [self.dataDict valueForKey:key];
我正在关注字典作为 JSON 响应
Lease = {
"KINDERSLEY KERROBERT" =
(
{
"lease_code" = 37;
},
{
"lease_code" = 38;
}
);
LLOYDMINSTER =
(
{
"lease_code" = 68;
},
{
"lease_code" = 69;
}
);
"SOUTHEASTERN SASKATCHEWAN" =
(
{
"lease_code" = 1;
},
{
"lease_code" = 2;
}
);
"SWIFT CURRENT" =
(
{
"lease_code" = 32;
},
{
"lease_code" = 33;
}
);
};
我想排序如下:
Lease = {
"SOUTHEASTERN SASKATCHEWAN" =
(
{
"lease_code" = 1;
},
{
"lease_code" = 2;
}
);
"SWIFT CURRENT" =
(
{
"lease_code" = 32;
},
{
"lease_code" = 33;
}
);
"KINDERSLEY KERROBERT" =
(
{
"lease_code" = 37;
},
{
"lease_code" = 38;
}
);
LLOYDMINSTER =
(
{
"lease_code" = 68;
},
{
"lease_code" = 69;
}
);
};
我写了下面的代码来排序字典:
- (NSArray *)sortKeysByIntValue:(NSDictionary *)dictionary {
NSArray *sortedKeys = [dictionary keysSortedByValueUsingComparator:^NSComparisonResult(NSArray *obj1, NSArray *obj2) {
NSString* key1 = [[obj1 objectAtIndex:0] objectForKey:@"lease_code"];
NSString* key2 = [[obj2 objectAtIndex:0] objectForKey:@"lease_code"];
return [key1 compare:key2];
}];
return sortedKeys;
}
它给了我想要的 sortedKeys,但是在使用下面的代码重建 NSMutableDictionary
之后:
NSArray *sortedKeys = [self sortKeysByIntValue:dictionary];
NSMutableDictionary *sortedDictionary = [[NSMutableDictionary alloc] init];
for (NSString *key in sortedKeys) {
[sortedDictionary setObject:dictionary[key] forKey:key];
}
它再次未排序,如响应中所示。
请帮帮我
NSArray *sortedKeys = [self sortKeysByIntValue:dictionary];
NSMutableArray* values = [NSMutableArray array];
for (NSString* key in sortedKeys)
{
[values addObject:dictionary[key]];
}
现在您已经对键和值进行了排序。这正是您所需要的
字典总是未排序的。它们用作键值存储来查找数据。您应该将数据的顺序与字典分开。你需要你已经拥有的字典和一个排序的键数组。
@property NSDictionary *dataDict;
@property NSArray *sortedKeys;
self.sortedKeys = [self sortKeysByIntValue:self.dataDict];
在 UITableViewDataSource
方法中,您将首先使用索引查询数组,获取键,然后从字典中检索数据。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger row = [indexPath row];
NSString *key = [self.sortedkeys objectAtIndex:row];
NSObject *dataObject = [self.dataDict valueForKey:key];