I have array containing dictionary like below:
(
{
"act_priority" = B1;
}
{
"act_priority" = "";
}
{
"act_priority" = A;
}
{
"act_priority" = A3;
}
{
"act_priority" = B;
}
{
"act_priority" = A2;
}
{
"act_priority" = A1;
}
{
"act_priority" = "";
}
)
I would like to sort in alphabetically and numerically both way:
(
{
"act_priority" = A1;
}
{
"act_priority" = A2;
}
{
"act_priority" = A3;
}
{
"act_priority" = B1;
}
{
"act_priority" = A;
}
{
"act_priority" = B;
}
{
"act_priority" = "";
}
{
"act_priority" = "";
}
)
What i had tried is below:
NSArray* sorted = [activityArray sortedArrayUsingComparator:(NSComparator)^(NSDictionary *item1, NSDictionary *item2) {
NSString *score1 = [item1 objectForKey:#"act_priority"];
NSString *score2 = [item2 objectForKey:#"act_priority"];
return [score1 compare:score2 options:NSNumericSearch];
}];
Also:
NSSortDescriptor *Sorter = [[NSSortDescriptor alloc] initWithKey:#"act_priority" ascending:YES];
[activityArray sortUsingDescriptors:[NSArray arrayWithObject:Sorter]];
but it give me like
(
{
"act_priority" = "";
}
{
"act_priority" = "";
}
{
"act_priority" = A;
}
{
"act_priority" = B;
}
{
"act_priority" = A1;
}
{
"act_priority" = A2;
}
{
"act_priority" = A3;
}
{
"act_priority" = B1;
}
)
Here is what I think you want. I gave also an sample to test.
The main point is what you do inside the block.
NSArray *array = #[#{#"act_priority":#"B1"},
#{#"act_priority":#""},
#{#"act_priority":#"A"},
#{#"act_priority":#"A3"},
#{#"act_priority":#"B"},
#{#"act_priority":#"A2"},
#{#"act_priority":#"A1"},
#{#"act_priority":#""}];
NSArray *sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *obj1, NSDictionary *obj2)
{
NSString *string1 = [obj1 objectForKey:#"act_priority"];
NSString *string2 = [obj2 objectForKey:#"act_priority"];
if ([string1 length] == 0) //To put the #"" at the end
return NSOrderedDescending;
else if ([string2 length] == 0) //To put the #"" at the end
return NSOrderedAscending;
else
{
BOOL string1HasSuffixNumber = [self hasSuffixNumber:string1]; //If string1 has a number at the end
BOOL string2HasSuffixNumber = [self hasSuffixNumber:string2]; //If string2 has a number at the end
if (string1HasSuffixNumber && !string2HasSuffixNumber)
return NSOrderedAscending; //Put the string2 at the end
else if (!string1HasSuffixNumber && string2HasSuffixNumber)
return NSOrderedDescending; //Put the string1 at the end
else
return [string1 compare:string2 options:NSCaseInsensitiveSearch]; //Other option can be used, if you want case sensitive one for example, or not, etc.
}
}];
NSLog(#"SortedArray: %#", sortedArray);
With this your "special" method:
-(BOOL)hasSuffixNumber:(NSString *)string
{
if ([string length] < 1)
return FALSE; //"Security added, but in your use case you shouldn't go there;
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:#"0123456789"]; //Explicit the character set in case you want to add some changes
if ([[string substringFromIndex:[string length]-1] rangeOfCharacterFromSet:set].location != NSNotFound)
return TRUE;
else
return FALSE;
}
Output:
>SortedArray: (
{
"act_priority" = A1;
},
{
"act_priority" = A2;
},
{
"act_priority" = A3;
},
{
"act_priority" = B1;
},
{
"act_priority" = A;
},
{
"act_priority" = B;
},
{
"act_priority" = "";
},
{
"act_priority" = "";
} )
Related
I have seen many of the answers but didn't get my answer. So that's why I decide to post a question. If anybody can find the helpful link or answer will be helpful.
Here is my array of dictionary:
<__NSArrayM 0x283ba04e0>(
{
failvalues = (
"Check 1"
);
fieldname = "Check 3";
fieldvalue = (
"Check 1",
"Check 2"
);
showtype = mandatory;
tagvalue = 0;
},
{
failvalues = (
Fail
);
fieldname = "Dropdown 3";
fieldvalue = (
Fail
);
showtype = mandatory;
tagvalue = 1;
},
{
failvalues = (
"Check 1",
"Check 4"
);
fieldname = "Check 4";
fieldvalue = (
"Check 1",
"Check 2"
);
showtype = mandatory;
tagvalue = 2;
})
So I want to check if the fieldvalue contains failvalues or not.
Below is the code which I have tried but it doesn't seem to work:
for (int i = 0; i< [arrFields count]; i++) {
if ([[[arrFields objectAtIndex:i]objectForKey:#"failvalues"] isKindOfClass:[NSArray class]]) {
if (![[[[arrFields objectAtIndex:i] objectForKey:#"failvalues"] objectAtIndex:0] isEqualToString:#""]) {
NSLog(#"Field Values %#",[[arrFields objectAtIndex:i]objectForKey:#"fieldvalue"]);
NSArray *failValues = [[arrFields objectAtIndex:i] objectForKey:#"failvalues"];
for (int j = 0; j < [failValues count]; j++) {
if ([failValues containsObject:[[arrFields objectAtIndex:i]objectForKey:#"fieldvalue"]]) {
NSLog(#"Contains %#",[[arrFields objectAtIndex:i]objectForKey:#"fieldvalue"]);
} else {
NSLog(#"No fail values");
}
}
} else {
NSLog(#"No Fail Fields");
}
} else {
NSLog(#"Not an array");
}
}
EDIT:
This one I have tried but how to break both the loops
for (int i = 0; i< [arrFields count]; i++) {
NSArray *fieldValues = [[arrFields objectAtIndex:i]objectForKey:#"fieldvalue"];
NSArray *failValues = [[arrFields objectAtIndex:i] objectForKey:#"failvalues"];
if (![[[[arrFields objectAtIndex:i] objectForKey:#"failvalues"] objectAtIndex:0] isEqualToString:#""]) {
//NSLog(#"Field Values %#",[[arrFields objectAtIndex:i]objectForKey:#"fieldvalue"]);
for(NSString *value in fieldValues){
if ([failValues containsObject:value]) {
NSLog(#"Contains %#",value);
scanStatus = TRUE;
return;
}
}
} else {
NSLog(#"No Fail Fields");
}
}
Thanks in advance!
This is a classic use case for the feared and dreaded goto. It's well known that goto can create disastrously messy code, but in cases like this it'll make things cleaner. You have to know when to use goto, and definitely use it sparingly. But here's how I'd write your code:
BOOL found = NO;
for (NSDictionary *dict in arrFields)
{
NSArray *fieldValues = dict[#"fieldvalue"];
NSArray *failValues = dict[#"failvalues"];
if (![failValues[0] isEqualToString:#""]) {
for (NSString *value in fieldValue) {
if ([failValues containsObject:value]) {
NSLog(#"Contains %#",value);
found = YES;
goto leaveLoops;
}
}
}
}
leaveLoops:
if (found) NSLog(#"Found one.");
else NSLog(#"Didn't find one.");
And if you cannot bring yourself to use goto (you wouldn't be alone,) here's a gotoless alternative:
BOOL found = NO;
for (NSDictionary *dict in arrFields)
{
NSArray *fieldValues = dict[#"fieldvalue"];
NSArray *failValues = dict[#"failvalues"];
if (![failValues[0] isEqualToString:#""]) {
for (NSString *value in fieldValue) {
if ([failValues containsObject:value]) {
NSLog(#"Contains %#",value);
found = YES;
break;
}
}
if (found) break;
}
}
if (found) NSLog(#"Found one.");
else NSLog(#"Didn't find one.");
I am storing my json response in a NSDictionary and this dictionary contains various array, so I want to replace all the null values with #"" empty string.
{
Specialities = (
{
ApprovalStatus = Unapproved;
CurrencyCode = "<null>";
Packages = (
{
AssetId = 157965;
BasePrice = 10000;
BookingAdvance = 100;
Currency = INR;
Details = "<null>";
DiscountedPrice = 9000;
Id = 16579;
IsBestOffer = 1;
IsPopular = 1;
LineItems = (
{
IconClass = "fa-check";
Text = "A DVD with all edited and unedited images";
}
);
PackageVersion = 123955;
PriceUnit = 3;
Quantity = 4;
SpecialityId = 22;
Status = Rejected;
Tags = (
53
);
TermsAndConditions = "<null>";
Title = Test;
}
);
Photos = (
157965,
157964
);
ServiceDescription = 43534;
Speciality = 22;
SpecialityName = "Wedding Photographer";
UserFRPs = (
{
AssetId = 157965;
CurrencyCode = INR;
DiscountedPrice = 800;
FRPId = 13;
Id = 4559;
Price = 1000;
SpecialityId = 22;
Status = Active;
},
{
AssetId = 565441;
CurrencyCode = INR;
DiscountedPrice = 9000;
FRPId = 18;
Id = 5559;
Price = 10000;
SpecialityId = 22;
Status = Active;
}
);
Videos = (
{
VideoId = DaWOguXZbNA;
VideoLink = "http://www.youtube.com/watch?v=DaWOguXZbNA";
VideoType = YouTube;
},
{
VideoId = DGVJtAHzzDQ;
VideoLink = "http://www.youtube.com/watch?v=DGVJtAHzzDQ";
VideoType = YouTube;
},
{
VideoId = "_zxKLZR-xuk";
VideoLink = "http://www.youtube.com/watch?v=_zxKLZR-xuk";
VideoType = YouTube;
},
{
VideoId = 5SkBZcvuuQs;
VideoLink = "http://www.youtube.com/watch?v=5SkBZcvuuQs";
VideoType = YouTube;
},
{
VideoId = "H_Xi-lVB4Zw";
VideoLink = "http://www.youtube.com/watch?v=H_Xi-lVB4Zw";
VideoType = YouTube;
},
{
VideoId = TWhSjpsGvPQ;
VideoLink = "http://www.youtube.com/watch?v=TWhSjpsGvPQ";
VideoType = YouTube;
},
{
VideoId = N2CJrhHEydA;
VideoLink = "http://www.youtube.com/watch?v=N2CJrhHEydA";
VideoType = YouTube;
},
{
VideoId = Lq6faQVYcwY;
VideoLink = "http://www.youtube.com/watch?v=Lq6faQVYcwY";
VideoType = YouTube;
},
{
VideoId = v8WjMiodcKo;
VideoLink = "http://www.youtube.com/watch?v=v8WjMiodcKo";
VideoType = YouTube;
}
);
},
{
ApprovalStatus = Unapproved;
CurrencyCode = "<null>";
Packages = "<null>";
Photos = (
157967
);
ServiceDescription = Ddhd;
Speciality = 37;
SpecialityName = "Hair and Makeup Stylist";
UserFRPs = (
{
AssetId = 157967;
CurrencyCode = INR;
DiscountedPrice = 900;
FRPId = 34;
Id = 4560;
Price = 1000;
SpecialityId = 37;
Status = Active;
}
);
Videos = (
{
VideoId = "onvkllwM-OI";
VideoLink = "http://www.youtube.com/watch?v=onvkllwM-OI";
VideoType = YouTube;
},
{
VideoId = "_-cRVdTW2s8";
VideoLink = "http://www.youtube.com/watch?v=_-cRVdTW2s8";
VideoType = YouTube;
},
{
VideoId = DGVJtAHzzDQ;
VideoLink = "http://www.youtube.com/watch?v=DGVJtAHzzDQ";
VideoType = YouTube;
}
);
},
{
ApprovalStatus = Unapproved;
CurrencyCode = "<null>";
Packages = "<null>";
Photos = (
157963,
157962,
157961
);
ServiceDescription = Test;
Speciality = 55;
SpecialityName = Transport;
UserFRPs = "<null>";
Videos = (
{
VideoId = "cRchvv_dB2c";
VideoLink = "http://www.youtube.com/watch?v=cRchvv_dB2c";
VideoType = YouTube;
},
{
VideoId = "onvkllwM-OI";
VideoLink = "http://www.youtube.com/watch?v=onvkllwM-OI";
VideoType = YouTube;
},
{
VideoId = DGVJtAHzzDQ;
VideoLink = "http://www.youtube.com/watch?v=DGVJtAHzzDQ";
VideoType = YouTube;
}
);
},
{
ApprovalStatus = Unapproved;
CurrencyCode = "<null>";
Packages = "<null>";
Photos = "<null>";
ServiceDescription = Baby;
Speciality = 5;
SpecialityName = "Children/Babies Photographer";
UserFRPs = "<null>";
Videos = (
{
VideoId = "cRchvv_dB2c";
VideoLink = "http://www.youtube.com/watch?v=cRchvv_dB2c";
VideoType = YouTube;
},
{
VideoId = DGVJtAHzzDQ;
VideoLink = "http://www.youtube.com/watch?v=DGVJtAHzzDQ";
VideoType = YouTube;
}
);
},
}
I want all null values to be replace by empty string.
Then just do that
NSString *json = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:yourDictionary options:0 error:nil] encoding: NSUTF8StringEncoding];
NSString *jsonWithoutNulls = [json stringByReplacingOccurrencesOfString:#"<null>" withString:#""];
NSData *data = [jsonWithoutNulls dataUsingEncoding:NSUTF8StringEncoding]
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
I prefer to use Macros to determine null values.
#define IS_NULL(value) (value != nil && value != Nil && value != NULL && value != (id)[NSNull null])
and I invoke it like
if (IS_NULL(CurrencyCode))
{
//insert ""
}else
{
//do necessary updates
}
OR you can also use the following method ,
-(BOOL) isNull: (NSString*)value{
if ([value isEqualToString:#"<null>"]){
return false;
}
return true;
}
and you can invoke it like,
if (isNull(currencyType))
{
//insert #""
}else{
//do necessary updates
}
Try this this is mine code i am using i hope it would be helpful!!
func checkDictionary(let dict:NSMutableDictionary)
{
let keys = Array(dict.allKeys)
for i in keys
{
let checkvalue = dict.valueForKey(i as! String)
if checkvalue! .isKindOfClass(NSNull)
{
dict.setObject("", forKey: i as! NSString)
}
else if checkvalue!.isKindOfClass(NSDictionary)
{
let dic = checkvalue as! NSDictionary
let dicts = dic.mutableCopy()
self.checkDictionary(dicts as! NSMutableDictionary)
dict.setObject(dicts, forKey: i as! NSString)
}
else if checkvalue! .isKindOfClass(NSArray)
{
let keys2 = checkvalue as! NSArray
let keys1 = keys2.mutableCopy() as! NSMutableArray
dict.setObject(keys1, forKey: i as! NSString)
for j in keys1
{
if j .isKindOfClass(NSNull)
{
keys1.replaceObjectAtIndex(keys1.indexOfObject(j), withObject:"")
}
if j.isKindOfClass(NSDictionary)
{
let dic = j as! NSDictionary
let dicts = dic.mutableCopy()
keys1.replaceObjectAtIndex(keys1.indexOfObject(j), withObject: dicts)
self .checkDictionary(dicts as! NSMutableDictionary)
}
}
}
}
}
In objective-C Try this!!
-(void)CheckDictionary:(NSMutableDictionary *)dic
{
NSArray *Arr = [dic allKeys];
for (int i = 0; i<Arr.count; i++)
{
if ([[dic valueForKey:[Arr objectAtIndex:i]] isKindOfClass:[NSNull class]])
{
[dic setObject:#"" forKey:[Arr objectAtIndex:i]];
}
else if ([[dic valueForKey:[Arr objectAtIndex:i]] isKindOfClass:[NSDictionary class]])
{
NSMutableDictionary *dict = [[dic valueForKey:[Arr objectAtIndex:i]] mutableCopy];
[dic setObject:dict forKey:[Arr objectAtIndex:i]];
[self CheckDictionary:dict];
}
else if ([[dic valueForKey:[Arr objectAtIndex:i]] isKindOfClass:[NSMutableArray class]])
{
NSMutableArray *Arr12 = [dic valueForKey:[Arr objectAtIndex:i]];
for (int j = 0; j<Arr12.count; j++)
{
if ([[Arr12 objectAtIndex:j] isKindOfClass:[NSDictionary class]])
{
NSDictionary *dict123 = [Arr12 objectAtIndex:j];
NSMutableDictionary *dict = [dict123 mutableCopy];
[Arr12 replaceObjectAtIndex:j withObject:dict];
[self CheckDictionary:dict];
}
}
}
}
}
And Just pass the whole dictionary when you call this method I hope it would be helpful!!
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:responseObject options: NSJSONReadingMutableContainers error: nil];
if (dictionary)
{
NSMutableDictionary *dict = [dictionary mutableCopy];
[self CheckDictionary:dict];
dictionary = [NSDictionary dictionaryWithDictionary:dict];
}
This is how we do it
#interface NSMutableArray (JSON)
- (void)recursivelyRemoveNulls;
#end
#implementation NSMutableArray (JSON)
- (void)recursivelyRemoveNulls
{
[self enumerateObjectsUsingBlock:^(id value, NSUInteger __unused idx, BOOL __unused *nestedStop)
{
if ([value isKindOfClass:[NSDictionary class]])
{
NSMutableDictionary *modifiedValue = [NSMutableDictionary dictionaryWithDictionary:value];
[modifiedValue recursivelyRemoveNulls];
[self removeObject:value];
[self addObject:modifiedValue];
}
else if ([value isKindOfClass:[NSArray class]])
{
NSMutableArray *modifiedValue = [NSMutableArray arrayWithArray:value];
[modifiedValue recursivelyRemoveNulls];
[self removeObject:value];
[self addObject:modifiedValue];
}
}];
}
#end
#interface NSMutableDictionary (JSON)
- (void)recursivelyRemoveNulls;
#end
#implementation NSMutableDictionary (JSON)
- (void)recursivelyRemoveNulls
{
[self enumerateKeysAndObjectsUsingBlock:^(NSString *key, id value, BOOL __unused *stop)
{
if (value == [NSNull null] || value == nil)
{
[self removeObjectForKey:key];
}
else if ([value isKindOfClass:[NSDictionary class]])
{
NSMutableDictionary *modifiedValue = [NSMutableDictionary dictionaryWithDictionary:value];
[modifiedValue recursivelyRemoveNulls];
self[key] = modifiedValue;
}
else if ([value isKindOfClass:[NSArray class]])
{
NSMutableArray *modifiedValue = [NSMutableArray arrayWithArray:value];
[modifiedValue recursivelyRemoveNulls];
self[key] = modifiedValue;
}
}];
}
#end
The short code you can use here:-
NSString * newValue=[self isNotNull:[your Object here]] ? [your Object here] : #"Value that you want to replace";
- (BOOL)isNull:(NSObject *)object {
if (!object) return YES;
else if (object == [NSNull null]) return YES;
else if ([object isKindOfClass:[NSString class]]) {
return ([((NSString *)object)isEqualToString : #""]
|| [((NSString *)object)isEqualToString : #"null"]
|| [((NSString *)object)isEqualToString : #"<null>"]
|| [((NSString *)object)isEqualToString : #"(null)"]
);
}
return NO;
}
- (BOOL)isNotNull:(NSObject *)object {
return ![self isNull:object];
}
I'm trying to sort my arrays with a particular sequence using an NSComparisonResult. I'm unable to figure out how to achieve the sequence I'm wanting.
I'm trying to weight Emojis towards the top (sequence of Emojis doesn't matter), followed by A-Z letters with giving a weight towards lowercase before uppercase, followed by numbers, followed by punctuation, then by symbols, and whatever else after that I dont care about at this point. I've gotten pretty close so far, but am still coming up short with what I want.
The sequence I'm trying to achieve would look like this as the output:
("\Ud83d\Ude03",
a,
A,
aa,
aA,
ab,
aB,
a1,
A1,
1,
01,
11,
001,
0001,
1001,
"#",
"#a",
"#1",
"$12",
"$0012")
Based upon this array as the input:
#[ #"a", #"aA", #"aa", #"A", #"aB", #"11", #"1001", #"ab", #"001", #"01",
#"a1", #"A1", #"😃", #"0001", #"1", #"#", #"$12", #"$0012", #"#a", #"#1" ];
But this is the output I'm getting:
("\Ud83d\Ude03",
a,
A,
aA,
aa,
aB,
ab,
a1,
A1,
0001,
001,
01,
1,
1001,
11,
"#a",
"#1",
"$0012",
"$12",
"#")
Code:
- (NSArray *)sortedArray:(NSArray *)input
{
NSArray *newArray = [input sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
NSString *nameOne = obj1;
NSString *nameTwo = obj2;
NSString *startOne;
NSString *startTwo;
NSInteger currentIndex = 0;
NSInteger maxIndex = (nameOne.length < nameTwo.length) ? nameOne.length : nameTwo.length;
NSCharacterSet *decimalDigitCharSet = [NSCharacterSet decimalDigitCharacterSet];
NSCharacterSet *punctuationCharSet = [NSCharacterSet punctuationCharacterSet];
NSCharacterSet *symbolCharSet = [NSCharacterSet symbolCharacterSet];
NSMutableCharacterSet *nonPriorityCharSet = [[NSMutableCharacterSet alloc]init];
[nonPriorityCharSet formUnionWithCharacterSet:punctuationCharSet];
[nonPriorityCharSet formUnionWithCharacterSet:symbolCharSet];
do
{
if (currentIndex < maxIndex)
{
startOne = [nameOne substringWithRange:NSMakeRange(currentIndex, 1)];
startTwo = [nameTwo substringWithRange:NSMakeRange(currentIndex, 1)];
currentIndex++;
}
else
{
if (nameOne.length == nameTwo.length)
{
return NSOrderedSame;
}
else
{
return (nameOne.length < nameTwo.length) ? NSOrderedAscending : NSOrderedDescending;
}
}
}
while ([startOne isEqualToString:startTwo]);
{
NSRange rangeOne = [startOne rangeOfCharacterFromSet:nonPriorityCharSet];
NSRange rangeTwo = [startTwo rangeOfCharacterFromSet:nonPriorityCharSet];
if (rangeOne.length > 0 || rangeTwo.length > 0)
{
return (rangeOne.length > 0) ? NSOrderedDescending : NSOrderedAscending;
}
NSRange decimalRangeOne = [startOne rangeOfCharacterFromSet:decimalDigitCharSet];
NSRange decimalRangeTwo = [startTwo rangeOfCharacterFromSet:decimalDigitCharSet];
if (decimalRangeOne.length > 0 || decimalRangeTwo.length > 0)
{
if (decimalRangeOne.length == decimalRangeTwo.length)
{
return (startOne.intValue > startTwo.intValue) ? NSOrderedDescending : NSOrderedAscending;
}
else if (decimalRangeOne.length > decimalRangeTwo.length)
{
return NSOrderedDescending;
}
else if (decimalRangeTwo.length > decimalRangeOne.length)
{
return NSOrderedAscending;
}
}
}
return [nameOne localizedCaseInsensitiveCompare:nameTwo];
}];
return newArray;
}
You started well. But you didn't properly check for all the rules that you have set. I have created some categories based on you rules, and sort using them.
- (NSArray *)sortedArray:(NSArray *)input
{
__block id blocksafeSelf = self;
NSArray *newArray = [input sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString *nameOne = obj1;
NSString *nameTwo = obj2;
NSInteger currentIndex = 0;
unichar charOne = [nameOne characterAtIndex:0];
unichar charTwo = [nameTwo characterAtIndex:0];
short maxLength = MIN(nameOne.length, nameTwo.length);
do {
charOne = [nameOne characterAtIndex:currentIndex];
charTwo = [nameTwo characterAtIndex:currentIndex];
currentIndex ++;
}
while (charOne == charTwo && currentIndex < maxLength);
short oneCategory = [blocksafeSelf getCharCategory:charOne];
short twoCategory = [blocksafeSelf getCharCategory:charTwo];
if (oneCategory != twoCategory) {
return oneCategory > twoCategory;
}
else if (oneCategory != 1) {
if (nameOne.length != nameTwo.length) {
return nameOne.length > nameTwo.length;
}
else {
return charOne > charTwo;
}
}
else {
if (nameOne.length != nameTwo.length) {
return nameOne.length > nameTwo.length;
}
else {
oneCategory = [blocksafeSelf getLetterCategory:charOne];
twoCategory = [blocksafeSelf getLetterCategory:charTwo];
if (oneCategory == twoCategory) {
return charOne > charTwo;
}
else {
unichar tempCharOne = oneCategory == 7 ? charOne + 32 : charOne;
unichar tempCharTwo = twoCategory == 7 ? charTwo + 32 : charTwo;
if (tempCharOne != tempCharTwo) {
return tempCharOne > tempCharTwo;
}
else {
return oneCategory > twoCategory;
}
}
}
}
return [nameOne localizedCaseInsensitiveCompare:nameTwo];
}];
return newArray;
}
- (short)getCharCategory:(unichar)character {
if (character > 255) { // emoji
return 0;
}
NSCharacterSet *letterCaseCharSet = [NSCharacterSet letterCharacterSet];
if ([letterCaseCharSet characterIsMember:character]) return 1;
NSCharacterSet *decimalDigitCharSet = [NSCharacterSet decimalDigitCharacterSet];
if ([decimalDigitCharSet characterIsMember:character]) return 2;
NSCharacterSet *punctuationCharSet = [NSCharacterSet punctuationCharacterSet];
if ([punctuationCharSet characterIsMember:character]) return 3;
NSCharacterSet *symbolCharSet = [NSCharacterSet symbolCharacterSet];
if ([symbolCharSet characterIsMember:character]) return 4;
return 5;
}
- (short)getLetterCategory:(unichar)character {
NSCharacterSet *lowerCaseCharSet = [NSCharacterSet lowercaseLetterCharacterSet];
if ([lowerCaseCharSet characterIsMember:character]) return 6;
return 7;
}
I have a large NSDictionary. Fx.
"m:GetFolderResponse" = {
"m:ResponseMessages" = {
"m:GetFolderResponseMessage" = {
ResponseClass = Success;
"m:Folders" = {
"t:CalendarFolder" = {
"t:ChildFolderCount" = {
text = 0;
};
"t:DisplayName" = {
text = Calendar;
};
"t:FolderId" = {
ChangeKey = "AgAAABYAAABGewbOYWpKSrW/k23iIoFkAPJWd7/8";
Id = "AAMkADkwOWE2NjEyLTMwZWQtNGYyMy05OGQ1LWZjZjFkZGY5MTBhMAAuAAAAAAC1cjo8jkv5SKjQt5WaSmd1AQBGewbOYWpKSrW/k23iIoFkAPJWc0NrAAA=";
};
};
};
"m:ResponseCode" = {
text = NoError;
};
};
};
"xmlns:m" = "http://schemas.microsoft.com/exchange/services/2006/messages";
"xmlns:t" = "http://schemas.microsoft.com/exchange/services/2006/types";
};
}
As you might have guessed, there can be multiple in the m:Folders. Therefore I would like to find m:Folders child, where t:DisplayName is equal to a variable value. How can I do this?
- (void)filterMutableDictionary:(NSDictionary*)aDictionary andKeyName:(NSString *)keyName
{
if ([keyName isEqualToString:#"t:CalendarFolder"]) {
if ([[[aDictionary objectForKey:#"t:DisplayName"] objectForKey:#"text"] isEqualToString:searchCalendarName]) {
NSDictionary *temp = [aDictionary objectForKey:#"t:FolderId"];
CalID = [temp objectForKey:#"Id"];
CalChangeID = [temp objectForKey:#"ChangeKey"];
}
}
// enumerate key/values, filtering appropriately, recursing as necessary
NSLog(#"%#",aDictionary);
[aDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL *stop) {
if ([value isKindOfClass: [NSMutableDictionary class]] || [value isKindOfClass: [NSDictionary class]]) {
[self filterMutableDictionary: value andKeyName:key];
}
}];
}
I've been trying to sort an NSArray of NSDictionaries using a comparator, but I cannot seem to get the output I desire.
The output I'm trying to achieve is that A-Z usernames should come first in the sorted array, then usernames that start with a digit should come second in the sorted array, and lastly usernames that start with an underscore should be last in the sorted array. Any help is truly appreciated!
EDIT: It should be sorted so it looks consistent through the whole NSArray so that: _Anna comes before _Bob and _11Bob comes before _12Cary but after _09Bob
Example of desired output I'm looking for:
(
{
username = abcd;
},
{
username = Anna;
},
{
username = 01Bob;
},
{
username = 02Tob;
},
{
username = 03ZED;
},
{
username = 04_Hob;
},
{
username = 04_sob;
},
{
username = "_anna";
},
{
username = "_bob";
},
{
username = "_boc";
},
{
username = "_bocd12";
},
{
username = "_bocd13";
}
{
username = _01Bob;
},
{
username = _02Tob;
},
)
I hope that makes sense now.
Sample NSDictionary with an NSArray of NSDictionaries:
NSDictionary *dictionary = #{#"users":#[#{#"username":#"191anna"},#{#"username":#"_091bob"},#{#"username":#"Bob"},#{#"username":#"charlie"}]};
I'm trying by using this comparator:
NSArray *array = [[dictionary objectForKey:#"users"] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
NSString *f1 = [obj1 objectForKey:#"username"];
NSString *f2 = [obj2 objectForKey:#"username"];
NSString *s1 = [[obj1 objectForKey:#"username"]substringFromIndex:1];
NSString *s2 = [[obj2 objectForKey:#"username"]substringFromIndex:1];
if ([s1 rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location == [s2 rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location)
{
return [f1 localizedCaseInsensitiveCompare:f2];
}
else if ([s1 rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location != [s2 rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location)
{
return [f1 localizedCaseInsensitiveCompare:f2];
if ([s1 rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location == NSNotFound)
{
return NSOrderedDescending;
}
}
return NSOrderedAscending;
}];
But it gives me the following (not the way I want) sorted NSArray:
(
{
username = "_091bob";
},
{
username = 191anna;
},
{
username = Bob;
},
{
username = charlie;
}
)
Here's what I came up with. It's a touch long because it requires quite a bit of logic. It can likely be optimized further:
My Set Up:
NSArray * usernames = #[#"191anna", #"abcd", #"Anna", #"01Bob", #"02Tob", #"03ZED", #"04_rob", #"_anna", #"_bob", #"_boc", #"_bocd12", #"_bocd13", #"_01Bob", #"_02Tob"];
NSMutableArray * users = [NSMutableArray array];
for (NSString * username in usernames) {
[users addObject:#{#"username":username}];
}
NSDictionary * dictionary = #{#"users":users};
And The Sort:
NSArray *sortedArray = [dictionary[#"users"] sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
NSString *nameOne = obj1[#"username"];
NSString *nameTwo = obj2[#"username"];
NSString *startOne;
NSString *startTwo;
NSInteger currentIndex = 0;
NSInteger maxIndex = (nameOne.length < nameTwo.length) ? nameOne.length : nameTwo.length;
// Get our first differentiating letter
do {
if (currentIndex < maxIndex) {
startOne = [nameOne substringWithRange:NSMakeRange(currentIndex, 1)];
startTwo = [nameTwo substringWithRange:NSMakeRange(currentIndex, 1)];
currentIndex++;
}
else {
// Names are equal up to max length. Same length is same, else shorter word ascending. (bob above bobb)
if (nameOne.length == nameTwo.length) {
return NSOrderedSame;
}
else {
return (nameOne.length < nameTwo.length) ? NSOrderedAscending : NSOrderedDescending;
}
}
} while ([startOne isEqualToString:startTwo]);
// Prioritize underscores to bottom
NSCharacterSet * underscoreCharSet = [NSCharacterSet characterSetWithCharactersInString:#"_"];
NSRange underscoreRangeOne = [startOne rangeOfCharacterFromSet:underscoreCharSet];
NSRange underscoreRangeTwo = [startTwo rangeOfCharacterFromSet:underscoreCharSet];
if (underscoreRangeOne.length > 0 || underscoreRangeTwo.length > 0) {
// Something is underscored, put it on the bottom
return (underscoreRangeOne.length > 0) ? NSOrderedDescending : NSOrderedAscending;
}
// Prioritize numbers to bottom
NSRange decimalRangeOne = [startOne rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]];
NSRange decimalRangeTwo = [startTwo rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]];
if (decimalRangeOne.length > 0 || decimalRangeTwo.length > 0) {
// Something is numbered, put it on the bottom
if (decimalRangeOne.length == decimalRangeTwo.length) {
return (startOne.intValue > startTwo.intValue) ? NSOrderedDescending : NSOrderedAscending;
}
else if (decimalRangeOne.length > decimalRangeTwo.length) {
return NSOrderedDescending;
}
else if (decimalRangeTwo.length > decimalRangeOne.length) {
return NSOrderedAscending;
}
}
// Now, sort alphabetically
return [nameOne localizedCaseInsensitiveCompare:nameTwo];
}];
NSLog(#"SortedArray: %#", sortedArray);
Will log as:
abcd,
Anna,
01Bob,
02Tob,
03ZED,
"04_rob",
191anna,
"_anna",
"_bob",
"_boc",
"_bocd12",
"_bocd13",
"_01Bob",
"_02Tob"
You can optimize this further, but your sort logic would be like below.
NSArray *sorted = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString *name1 = [(NSDictionary *) obj1 objectForKey:NAME];
NSString *name2 = [(NSDictionary *) obj2 objectForKey:NAME];
if ([name1 characterAtIndex:0] == '_' && [name2 characterAtIndex:0] == '_')
{
return [name1 compare:name2 options:NSCaseInsensitiveSearch];
}
else if ([name1 characterAtIndex:0] == '_')
{
return NSOrderedDescending;
}
else if ([name2 characterAtIndex:0] == '_')
{
return NSOrderedAscending;
}
else if (([name1 intValue] && [name2 intValue]) || ([name1 characterAtIndex:0] == '0' && [name2 characterAtIndex:0] == '0'))
{
return [name1 compare:name2 options:NSCaseInsensitiveSearch];
}
else if ([name1 intValue] >0 || [name1 characterAtIndex:0] == '0')
{
return NSOrderedDescending;
}
else if ([name2 intValue]>0 || [name2 characterAtIndex:0] == '0')
{
return NSOrderedAscending;
}
else
{
return [name1 compare:name2 options:NSCaseInsensitiveSearch];
}
//return res;
}];