java - 如何对对象的arraylist进行排序? [英] How to sort an arraylist of objects java?
本文介绍了java - 如何对对象的arraylist进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我想要一个 java 中的对象数组.
So I want an arraylist of objects in java.
我有 object1.number
和 object2.number
、object3.number
等...但这些对象除了 number
,例如name
、distance
等...
I have object1.number
and object2.number
, object3.number
, etc... but those objects have other properties besides number
, such as name
, distance
, etc...
因此,如果它正在对 array
中的字符串进行排序,那么将一个字符串放入 temporal
并让另一个字符串代替它......但是在对象的 araryList
中,我该怎么做?
So if it was sorting a string in a array
it would just be, put a string in a temporal
and let the other string take its place... but in an araryList
of objects, how can I do it?
我可以将对象移动到数组的那个位置吗?
Can I just move objects to that position of the array?
谢谢.
推荐答案
实现你自己的比较器:
Arrays.sort(yourArray, new Comparator<YourClass>() {
@Override
public int compare(YourClass o1, YourClass o2) {
//compare object properties
}
});
这篇关于java - 如何对对象的arraylist进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文