ios - 使用比较器对 NSDictionaries 的 NSArray 进行排序
<p><p>我一直在尝试使用比较器对 NSDictionaries 的 NSArray 进行排序,但我似乎无法获得我想要的输出。 </p>
<p>我想要实现的输出是 A-Z 用户名应该排在排序数组的第一位,然后以数字开头的用户名应该排在排序数组的第二位,最后以下划线开头的用户名应该排在最后排序后的数组。任何帮助都非常感谢!</p>
<p><strong>编辑:应该对其进行排序,使其在整个 NSArray 中看起来一致,这样:_Anna 位于 _Bob 之前,_11Bob 位于 _12Cary 之前但在 _09Bob 之后</strong> </p>
<p>我正在寻找的期望输出示例:</p>
<pre><code>(
{
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;
},
)
</code></pre>
<p>我希望现在有意义。</p>
<p>带有 NSDictionaries 的 NSArray 的示例 NSDictionary:</p>
<pre><code>NSDictionary *dictionary = @{@"users":@[@{@"username":@"191anna"},@{@"username":@"_091bob"},@{@"username":@"Bob"},@{@"username":@"charlie"}]};
</code></pre>
<p>我正在尝试使用这个比较器:</p>
<pre><code>NSArray *array = [ sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2)
{
NSString *f1 = ;
NSString *f2 = ;
NSString *s1 = [substringFromIndex:1];
NSString *s2 = [substringFromIndex:1];
if (].location == ].location)
{
return;
}
else if (].location != ].location)
{
return;
if (].location == NSNotFound)
{
return NSOrderedDescending;
}
}
returnNSOrderedAscending;
}];
</code></pre>
<p>但它给了我以下(不是我想要的)排序的 NSArray:</p>
<pre><code>(
{
username = "_091bob";
},
{
username = 191anna;
},
{
username = Bob;
},
{
username = charlie;
}
)
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>这就是我想出的。它有点长,因为它需要相当多的逻辑。它可能会进一步优化:</p>
<p>我的设置:</p>
<pre><code>NSArray * usernames = @[@"191anna", @"abcd", @"Anna", @"01Bob", @"02Tob", @"03ZED", @"04_rob", @"_anna", @"_bob", @"_boc", @"_bocd12", @"_bocd13", @"_01Bob", @"_02Tob"];
NSMutableArray * users = ;
for (NSString * username in usernames) {
;
}
NSDictionary * dictionary = @{@"users":users};
</code></pre>
<p>排序:</p>
<pre><code>NSArray *sortedArray = 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 = ;
startTwo= ;
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 ();
// Prioritize underscores to bottom
NSCharacterSet * underscoreCharSet = ;
NSRange underscoreRangeOne = ;
NSRange underscoreRangeTwo = ;
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 = ];
NSRange decimalRangeTwo = ];
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;
}];
NSLog(@"SortedArray: %@", sortedArray);
</code></pre>
<p>将记录为:</p>
<pre><code>abcd,
Anna,
01Bob,
02Tob,
03ZED,
"04_rob",
191anna,
"_anna",
"_bob",
"_boc",
"_bocd12",
"_bocd13",
"_01Bob",
"_02Tob"
</code></pre></p>
<p style="font-size: 20px;">关于ios - 使用比较器对 NSDictionaries 的 NSArray 进行排序,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/23964377/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/23964377/
</a>
</p>
页:
[1]