根据浮点值对对象数组进行排序 [英] sorting an array of object with respect to the float value
本文介绍了根据浮点值对对象数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
for (PNPieChartDataItem *item in items) {// didnt work
NSArray *sortedArray;
sortedArray = [items sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
CGFloat first = [(PNPieChartDataItem*)a value];
CGFloat second = [(PNPieChartDataItem*)b value];
return [first compare:second];
}];
}
解决方案
您不需要for循环,sortedArrayUsingComparator
迭代值以进行比较:
NSArray *sortedArray = [items sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
CGFloat first = [(PNPieChartDataItem*)a value];
CGFloat second = [(PNPieChartDataItem*)b value];
return [first compare:second];
}];
}
使用sortedArray
.
im working with a project , where i receive an array of object, each object has a description of type string, and a value of type CGfloat. i need to sort them by value increasingly, and store them in a dicitonary. i tried this solution, but it didn't work. How do I sort an NSMutableArray with custom objects in it? any idea how to do it?
for (PNPieChartDataItem *item in items) {// didnt work
NSArray *sortedArray;
sortedArray = [items sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
CGFloat first = [(PNPieChartDataItem*)a value];
CGFloat second = [(PNPieChartDataItem*)b value];
return [first compare:second];
}];
}
解决方案
You don't need a for loop, sortedArrayUsingComparator
iterates on values to compare:
NSArray *sortedArray = [items sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
CGFloat first = [(PNPieChartDataItem*)a value];
CGFloat second = [(PNPieChartDataItem*)b value];
return [first compare:second];
}];
}
Use sortedArray
.
这篇关于根据浮点值对对象数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文