ios - 使用索引进行数字排序
<p><p>我想对数字进行升序排序,包括它们的索引,这里我已经实现了这样的排序。</p>
<pre><code>PriceArray = [
" 93",
" 112.8",
" 138.45",
" 127.25",
" 117.25",
" 114.45"
]
</code></pre>
<p>通过使用这个,</p>
<pre><code>NSArray *myArray = [priceArray sortedArrayUsingDescriptors:
@[[NSSortDescriptor sortDescriptorWithKey:@"doubleValue"
ascending:YES]]];
</code></pre>
<p>排序后这个数据就出来了</p>
<pre><code>[
" 93",
" 112.8",
" 114.45",
" 117.25",
" 127.25",
"138.45"
]
</code></pre>
<p>但我想对包括索引的数据进行排序</p>
<pre><code>[
[
" 93",
"0"
],
[
" 112.8",
"1"
],
[
" 114.45",
"5"
],
[
" 117.25",
"4"
],
[
" 127.25",
"3"
],
[
" 138.45",
"2"
]
]
</code></pre>
<p>你能建议我如何实现这个吗?谢谢。。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><pre><code>NSArray *priceArray =@[
@" 93",
@" 112.8",
@" 138.45",
@" 127.25",
@" 117.25",
@" 114.45"
];
NSMutableArray *output = ;
for(NSInteger i=0;i<;i++){
NSArray *dataWithIndex = @,@(i)];
;
}
NSArray *sorted = [output sortedArrayUsingComparator:^NSComparisonResult(id_Nonnull obj1, id_Nonnull obj2) {
return [ doubleValue]>[ doubleValue];
}];
NSLog(@"%@",sorted);
</code></pre></p>
<p style="font-size: 20px;">关于ios - 使用索引进行数字排序,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/41018082/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/41018082/
</a>
</p>
页:
[1]