[英]Sorting out an array List using Collections
I want to use the Comparator object of the Collections framework to sort out an ArrayList by an specific attribute. 我想使用Collections框架的Comparator对象通过特定属性对ArrayList进行排序。
I have an ArrayList called measureEviArray
this array contains objects type MeasureEvi
. 我有一个名为
measureEviArray
的ArrayList,该数组包含对象类型MeasureEvi
。
The class MeasureEvi
has an attribute called mCoordenate
which corresponds to a PointF data type. MeasureEvi
类具有一个称为mCoordenate
的属性,该属性与PointF数据类型相对应。
public class MeasureEvi {
private int mIndex; //
private int mOrderIndex;
private int eIndex; //
private TextView mTag; //
private PointF mCoordenate;
}
What I want is to sort out that array depending on the coordenates given to each object in X ( mCoordenate.x
) 我想要的是根据给X中每个对象的坐标(
mCoordenate.x
)整理该数组。
Is it possible to use Collections for this and get an Array Ordenate from the minor to the greatest in X axis? 是否可以为此使用Collections并从X轴上的次要数组到最大数组得到Ordenate?
I was trying to implement this way: 我试图以这种方式实现:
Collections.sort(measureEviArray, new Comparator<MeasureEvi>() {
@Override
public int compare(MeasureEvi measureEvi, MeasureEvi t1) {
return measureEvi.getmCoordenate().x.compareTo(t1.getmCoordenate().x);
}
});
but compareTo is not an Option, cannot resolve method. 但是compareTo不是Option,无法解析方法。
由于PointF
中的x
是float
,请使用Float.compare(float f1, float f2)
:
return Float.compare(measureEvi.getmCoordenate().x, t1.getmCoordenate().x);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.