字符串比对:NSOrderedAscending , NSOrderedSame, NSOrderedDescending

1、字符串比对结果:NSComparisonResult(在NSObjCRunTime.h中定义)

按照字符的ASCII值进行比对

NSString * str1 = @"abc";

NSString * str2 = @"abd";

NSString * str3 = @"ABC";

NSString * str4 = @"abc";

NSString * str5 = @"123";

那么,[str1 compare:str2] == NSOrderedAscending(升序)

[str2 compare:str1] == NSOrderedDescending(降序)

[str1 compare:str3] == NSOrderedDescending(降序)

[str1 compare:str4] == NSOrderedSame(同序)

[str1 compare:str5] == NSOrderedDescending(降序)



2、多个NSString对象按字符顺序进行排序

先进行字符串比对,然后将不符合排序要求的两个字符串对象交换位置

2.1 升序

if([str1 compare:str2] == NSOrderedDescending)

{

//To do

//str1与str2交换位置

}

2.2 降序

if([str1 compare:str2] == NSOrderedAscending)

{

//To do

//str1与str2交换位置

}



你可能感兴趣的:(字符串比对:NSOrderedAscending , NSOrderedSame, NSOrderedDescending)