简体   繁体   English

如何使用 x 轴值对数组元素进行排序?

[英]How to sort array elements using x-axis values?

Actually I am getting the label values and x-coordinate and I need to sort the label values according to x-coordinate values.实际上我得到了 label 值和 x 坐标,我需要根据 x 坐标值对 label 值进行排序。 Can you please any one give me code how to do it.你能请任何人给我代码如何做到这一点。 Thanks in advance..提前致谢..

This is one way to do it, you should be able to do it with keypaths a lot nicer.这是做到这一点的一种方法,您应该能够更好地使用键路径来做到这一点。

[labels sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    return [[NSNumber numberWithFloat:[(UIView *)obj1 frame].origin.x] compare:[NSNumber numberWithFloat:[(UIView *)obj2 frame].origin.x]];
}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM