按升序排序多维 NSMutableArray Objective - C
Sorting MultiDimentional NSMutableArray in Ascending Order Objective - C
我正在尝试使用键 @"Channel" 按升序对这个 NSMutableArray(带有字典对象)进行排序(有多个频道条目,为了便于阅读,我只是缩短了它):
我尝试使用以下方式进行排序:
NSSortDescriptor *channelDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"Channel" ascending:YES selector:@selector(compare:)];
NSArray *descriptors = @[channelDescriptor];
[self.tempArrayDuplicateDataForAppWatch sortUsingDescriptors:descriptors];
数据:
{
Airings = (
{
AiringTime = "2015-08-24T19:00:00Z";
AiringType = Unknown;
CC = 1;
Category = Movie;
Color = Color;
DSS = 0;
DVS = 0;
Dolby = 0;
Duration = 120;
HD = 0;
HDLevel = "HD Level Unknown";
LetterBox = 0;
MovieRating = R;
ProgramId = 20306212;
SAP = 0;
Sports = 0;
Stereo = 1;
Subcategory = horror;
TVRating = "TV-MA";
Title = Chernobyl;
}
);
CallLetters = Syfy;
Channel = 151;
ChannelImages = (
{
AspectRatio = "109:79";
ImageCaption = "Source logo";
ImageCredit = Syfy;
ImageCreditDisplay = false;
ImageExpiryDateTime = "<null>";
ImageFormat = jpg;
ImageFormatId = 144;
ImageHorizontalResolution = 720;
ImageId = 74975320;
ImageOwner = Syfy;
ImageTitle = Syfy;
ImageType = "Station Logo";
ImageUrl = ;
ImageVerticalResolution = 521;
LastUpdate = "2014-07-11T22:05:13Z";
ObjectId = 1232;
ObjectName = "Syfy (East)";
ObjectType = Source;
ParentImageId = 16795667;
}
);
ChannelSchedules = (
);
DisplayName = Syfy;
IconAvailable = 0;
IsChannelOverride = 0;
Order = 1510647;
ParentNetworkId = 0;
ServiceId = 82368;
SourceAttributeTypes = "";
SourceAttributes = 0;
SourceId = 1232;
SourceLongName = "Syfy (East)";
SourceType = Basic;
Type = "24-Hours";
},
{
Airings = (
{
AiringTime = "2015-08-24T19:00:00Z";
AiringType = Repeat;
CC = 1;
Category = Other;
Color = Color;
DSS = 0;
DVS = 0;
Dolby = 0;
Duration = 30;
HD = 0;
HDLevel = "HD Level Unknown";
LetterBox = 0;
ProgramId = 26463608;
SAP = 0;
SeriesId = 24828094;
Sports = 0;
Stereo = 1;
Subcategory = "game show";
TVRating = None;
Title = "Celebrity Game";
},
{
AiringTime = "2015-08-24T19:30:00Z";
AiringType = Repeat;
CC = 1;
Category = Other;
Color = Color;
DSS = 0;
DVS = 0;
Dolby = 0;
Duration = 30;
HD = 0;
HDLevel = "HD Level Unknown";
LetterBox = 0;
ProgramId = 26332661;
SAP = 0;
SeriesId = 24828094;
Sports = 0;
Stereo = 1;
Subcategory = "game show";
TVRating = None;
Title = "Celebrity Game";
},
{
AiringTime = "2015-08-24T20:00:00Z";
AiringType = Repeat;
CC = 1;
Category = Other;
Color = Color;
DSS = 0;
DVS = 0;
Dolby = 0;
Duration = 30;
HD = 0;
HDLevel = "HD Level Unknown";
LetterBox = 0;
ProgramId = 1055967;
SAP = 0;
Sports = 0;
Stereo = 0;
Subcategory = "game show";
TVRating = "TV-PG";
Title = "Family Feud";
},
{
AiringTime = "2015-08-24T20:30:00Z";
AiringType = Repeat;
CC = 1;
Category = Other;
Color = Color;
DSS = 0;
DVS = 0;
Dolby = 0;
Duration = 30;
HD = 0;
HDLevel = "HD Level Unknown";
LetterBox = 0;
ProgramId = 1055967;
SAP = 0;
Sports = 0;
Stereo = 0;
Subcategory = "game show";
TVRating = "TV-PG";
Title = "Family Feud";
}
);
CallLetters = WDAF;
Channel = 4;
ChannelImages = (
{
AspectRatio = "23:9";
ImageCaption = "92x36;darklight";
ImageCreditDisplay = false;
ImageExpiryDateTime = "<null>";
ImageFormat = jpg;
ImageFormatId = 97;
ImageHorizontalResolution = 800;
ImageId = 4065300;
ImageTitle = FOX;
ImageType = "Station Logo";
ImageUrl = ;
ImageVerticalResolution = 313;
LastUpdate = "2015-06-22T16:50:52Z";
ObjectId = 134;
ObjectName = WDAF;
ObjectType = Source;
ParentImageId = 16814381;
}
);
ChannelSchedules = (
);
DisplayName = WDAF;
IconAvailable = 1;
IsChannelOverride = 0;
Order = 40002;
ParentNetworkId = 3;
ServiceId = 82368;
SourceAttributeTypes = "";
SourceAttributes = 0;
SourceId = 134;
SourceLongName = WDAF;
SourceType = Broadcast;
Type = "24-Hours";
}
我意识到 @"Channel" 值是字符串格式,我不能使用 "compare:" 我想知道是否有更好的方法。非常感谢您的帮助!
对于具有键值的字典的 NSArray 的排序数组如下。
brandDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Channel" ascending:YES];
sortDescriptors = [NSArray arrayWithObject:brandDescriptor];
sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors];
我从 https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/SortDescriptors/Articles/Creating.html#//apple_ref/doc/uid/20001845-BAJEAIEE. Also, https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/KeyValueCoding.html 中提取的代码开始发挥作用,因为 sortedArrayUsingDescriptors
: 将发送一个 valueForKey
: 到 myArray 中的每个元素,然后使用标准比较器对返回值。
再次感谢您的帮助和建议。我终于弄明白了为什么 NSSortDescriptor 不起作用。 @"Channel" 的值是一个字符串,因此排序无法正常工作。我不得不从 NSMutableArray 中逐一提取每个 Dictionary,并将 @"Channel" 的值更改为 NSNumber/NSDecimalNumber,然后将旧字符串值替换为新值。然后我用 NSSortDescriptor 排序,它起作用了。
示例代码如下:
for (int i=0; i<[unsortedNSMutableArray count]; i++)
{
NSMutableDictionary *tempChannelInfo = [[NSMutableDictionary alloc] init];
tempChannelInfo = unsortedNSMutableArray[i];
NSString *tempChannelString = tempChannelInfo[@"Channel"];
NSNumber *convertedChannel = [NSDecimalNumber decimalNumberWithString:tempChannelString];
[tempChannelInfo setObject:convertedChannel forKey:@"Channel"];
[unsortedNSMutableArray replaceObjectAtIndex:i withObject:tempChannelInfo];
}
然后我排序:
NSSortDescriptor *channelDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"Channel" ascending:YES selector:@selector(compare:)];
NSArray *descriptors = @[channelDescriptor];
[unsortedNSMutableArray sortUsingDescriptors:descriptors];
再次感谢您的帮助和建议!
我正在尝试使用键 @"Channel" 按升序对这个 NSMutableArray(带有字典对象)进行排序(有多个频道条目,为了便于阅读,我只是缩短了它):
我尝试使用以下方式进行排序:
NSSortDescriptor *channelDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"Channel" ascending:YES selector:@selector(compare:)];
NSArray *descriptors = @[channelDescriptor];
[self.tempArrayDuplicateDataForAppWatch sortUsingDescriptors:descriptors];
数据:
{
Airings = (
{
AiringTime = "2015-08-24T19:00:00Z";
AiringType = Unknown;
CC = 1;
Category = Movie;
Color = Color;
DSS = 0;
DVS = 0;
Dolby = 0;
Duration = 120;
HD = 0;
HDLevel = "HD Level Unknown";
LetterBox = 0;
MovieRating = R;
ProgramId = 20306212;
SAP = 0;
Sports = 0;
Stereo = 1;
Subcategory = horror;
TVRating = "TV-MA";
Title = Chernobyl;
}
);
CallLetters = Syfy;
Channel = 151;
ChannelImages = (
{
AspectRatio = "109:79";
ImageCaption = "Source logo";
ImageCredit = Syfy;
ImageCreditDisplay = false;
ImageExpiryDateTime = "<null>";
ImageFormat = jpg;
ImageFormatId = 144;
ImageHorizontalResolution = 720;
ImageId = 74975320;
ImageOwner = Syfy;
ImageTitle = Syfy;
ImageType = "Station Logo";
ImageUrl = ;
ImageVerticalResolution = 521;
LastUpdate = "2014-07-11T22:05:13Z";
ObjectId = 1232;
ObjectName = "Syfy (East)";
ObjectType = Source;
ParentImageId = 16795667;
}
);
ChannelSchedules = (
);
DisplayName = Syfy;
IconAvailable = 0;
IsChannelOverride = 0;
Order = 1510647;
ParentNetworkId = 0;
ServiceId = 82368;
SourceAttributeTypes = "";
SourceAttributes = 0;
SourceId = 1232;
SourceLongName = "Syfy (East)";
SourceType = Basic;
Type = "24-Hours";
},
{
Airings = (
{
AiringTime = "2015-08-24T19:00:00Z";
AiringType = Repeat;
CC = 1;
Category = Other;
Color = Color;
DSS = 0;
DVS = 0;
Dolby = 0;
Duration = 30;
HD = 0;
HDLevel = "HD Level Unknown";
LetterBox = 0;
ProgramId = 26463608;
SAP = 0;
SeriesId = 24828094;
Sports = 0;
Stereo = 1;
Subcategory = "game show";
TVRating = None;
Title = "Celebrity Game";
},
{
AiringTime = "2015-08-24T19:30:00Z";
AiringType = Repeat;
CC = 1;
Category = Other;
Color = Color;
DSS = 0;
DVS = 0;
Dolby = 0;
Duration = 30;
HD = 0;
HDLevel = "HD Level Unknown";
LetterBox = 0;
ProgramId = 26332661;
SAP = 0;
SeriesId = 24828094;
Sports = 0;
Stereo = 1;
Subcategory = "game show";
TVRating = None;
Title = "Celebrity Game";
},
{
AiringTime = "2015-08-24T20:00:00Z";
AiringType = Repeat;
CC = 1;
Category = Other;
Color = Color;
DSS = 0;
DVS = 0;
Dolby = 0;
Duration = 30;
HD = 0;
HDLevel = "HD Level Unknown";
LetterBox = 0;
ProgramId = 1055967;
SAP = 0;
Sports = 0;
Stereo = 0;
Subcategory = "game show";
TVRating = "TV-PG";
Title = "Family Feud";
},
{
AiringTime = "2015-08-24T20:30:00Z";
AiringType = Repeat;
CC = 1;
Category = Other;
Color = Color;
DSS = 0;
DVS = 0;
Dolby = 0;
Duration = 30;
HD = 0;
HDLevel = "HD Level Unknown";
LetterBox = 0;
ProgramId = 1055967;
SAP = 0;
Sports = 0;
Stereo = 0;
Subcategory = "game show";
TVRating = "TV-PG";
Title = "Family Feud";
}
);
CallLetters = WDAF;
Channel = 4;
ChannelImages = (
{
AspectRatio = "23:9";
ImageCaption = "92x36;darklight";
ImageCreditDisplay = false;
ImageExpiryDateTime = "<null>";
ImageFormat = jpg;
ImageFormatId = 97;
ImageHorizontalResolution = 800;
ImageId = 4065300;
ImageTitle = FOX;
ImageType = "Station Logo";
ImageUrl = ;
ImageVerticalResolution = 313;
LastUpdate = "2015-06-22T16:50:52Z";
ObjectId = 134;
ObjectName = WDAF;
ObjectType = Source;
ParentImageId = 16814381;
}
);
ChannelSchedules = (
);
DisplayName = WDAF;
IconAvailable = 1;
IsChannelOverride = 0;
Order = 40002;
ParentNetworkId = 3;
ServiceId = 82368;
SourceAttributeTypes = "";
SourceAttributes = 0;
SourceId = 134;
SourceLongName = WDAF;
SourceType = Broadcast;
Type = "24-Hours";
}
我意识到 @"Channel" 值是字符串格式,我不能使用 "compare:" 我想知道是否有更好的方法。非常感谢您的帮助!
对于具有键值的字典的 NSArray 的排序数组如下。
brandDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Channel" ascending:YES];
sortDescriptors = [NSArray arrayWithObject:brandDescriptor];
sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors];
我从 https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/SortDescriptors/Articles/Creating.html#//apple_ref/doc/uid/20001845-BAJEAIEE. Also, https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/KeyValueCoding.html 中提取的代码开始发挥作用,因为 sortedArrayUsingDescriptors
: 将发送一个 valueForKey
: 到 myArray 中的每个元素,然后使用标准比较器对返回值。
再次感谢您的帮助和建议。我终于弄明白了为什么 NSSortDescriptor 不起作用。 @"Channel" 的值是一个字符串,因此排序无法正常工作。我不得不从 NSMutableArray 中逐一提取每个 Dictionary,并将 @"Channel" 的值更改为 NSNumber/NSDecimalNumber,然后将旧字符串值替换为新值。然后我用 NSSortDescriptor 排序,它起作用了。
示例代码如下:
for (int i=0; i<[unsortedNSMutableArray count]; i++)
{
NSMutableDictionary *tempChannelInfo = [[NSMutableDictionary alloc] init];
tempChannelInfo = unsortedNSMutableArray[i];
NSString *tempChannelString = tempChannelInfo[@"Channel"];
NSNumber *convertedChannel = [NSDecimalNumber decimalNumberWithString:tempChannelString];
[tempChannelInfo setObject:convertedChannel forKey:@"Channel"];
[unsortedNSMutableArray replaceObjectAtIndex:i withObject:tempChannelInfo];
}
然后我排序:
NSSortDescriptor *channelDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"Channel" ascending:YES selector:@selector(compare:)];
NSArray *descriptors = @[channelDescriptor];
[unsortedNSMutableArray sortUsingDescriptors:descriptors];
再次感谢您的帮助和建议!