java 按数值排序 List<>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11206495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sorting List<> by numeric value
提问by Luke Taylor
I've got a public List<FriendProfile> friends = new ArrayList<FriendProfile>();
. I initialize the friends list by reading the information from the server. The FriendProfile object contains a int called private int userPosition;
我有一个public List<FriendProfile> friends = new ArrayList<FriendProfile>();
. 我通过从服务器读取信息来初始化好友列表。FriendProfile 对象包含一个名为的 intprivate int userPosition;
Once the friends list has been initialized, I would like to sort the friends list by having the FriendProfile object with the highest userPosition
at index 0 of the list and then sort by accordingly, index 1 with the second highest userPosition
...
一旦朋友列表被初始化,我想通过userPosition
在列表的索引 0 处具有最高的 FriendProfile 对象对朋友列表进行排序,然后相应地排序,索引 1 具有第二高userPosition
......
I guess I could write an sorting algorithm, yet I'm looking for prewritten code (maybe the JDK has some methods to offer?)
我想我可以编写一个排序算法,但我正在寻找预先编写的代码(也许 JDK 提供了一些方法?)
Help is appreciated!
帮助表示赞赏!
回答by hmjd
Use Collections.sort()
and specify a Comparator
:
使用Collections.sort()
并指定一个Comparator
:
Collections.sort(friends,
new Comparator<FriendProfile>()
{
public int compare(FriendProfile o1,
FriendProfile o2)
{
if (o1.getUserPosition() ==
o2.getUserPosition())
{
return 0;
}
else if (o1.getUserPosition() <
o2.getUserPosition())
{
return -1;
}
return 1;
}
});
or have FriendProfile
implement Comparable<FriendProfile>
.
或有FriendProfile
实施Comparable<FriendProfile>
。
回答by Akhi
Implement Comparable Interface.
实现可比接口。
class FriendProfile implements Comparable<FriendProfile> {
private int userPosition;
@Override
public int compareTo(FriendProfile o) {
if(this.userPosition > o.userPosition){
return 1;
}
return 0;
}
}
Just Call the Collection.sort(List) method.
只需调用 Collection.sort(List) 方法。
FriendProfile f1=new FriendProfile();
f1.userPosition=1;
FriendProfile f2=new FriendProfile();
f2.userPosition=2;
List<FriendProfile> list=new ArrayList<FriendProfile>();
list.add(f2);
list.add(f1);
Collections.sort(list);
The List will be sorted.
列表将被排序。
回答by Pranav
Now no need to Boxing (i.e no need to Creating OBJECT
using new Operator use valueOf insted with compareTo of Collections.Sort..)
现在不需要装箱(即不需要OBJECT
使用新操作符创建使用 valueOf 与 Collections.Sort 的 compareTo 相结合..)
1)For Ascending order
1)对于升序
Collections.sort(temp, new Comparator<XYZBean>()
{
@Override
public int compare(XYZBean lhs, XYZBean rhs) {
return Integer.valueOf(lhs.getDistance()).compareTo(rhs.getDistance());
}
});
1)For Deascending order
1)对于降序
Collections.sort(temp, new Comparator<XYZBean>()
{
@Override
public int compare(XYZBean lhs, XYZBean rhs) {
return Integer.valueOf(rhs.getDistance()).compareTo(lhs.getDistance());
}
});
回答by OpenSauce
Use Collections.Sort
and write a custom Comparator
that compares based on userPosition
.
使用Collections.Sort
并编写一个Comparator
基于userPosition
.
回答by Dheeresh Singh
use Comparator with Collections.sort method
使用 Comparator 和 Collections.sort 方法
java.util.Collections.sort(list, new Comparator<FriendProfile >(){
public int compare(FriendProfile a, FriendProfile b){
if(a.getUserPosition() > b.getUserPosition()){
return 1;
}else if(a.getUserPosition() > b.getUserPosition()){
return -1;
}
return 0;
}
});
see this link
看到这个链接
回答by Ben Thurley
There are two ways to do this.
有两种方法可以做到这一点。
1. FriendProfile could implement the interface Comparable.
1. FriendProfile 可以实现接口Comparable。
public class FriendProfile implements Comparable<FriendProfile>
{
public int compareTo(FriendProfile that)
{
// Descending order
return that.userPosition - this.userPosition;
}
}
...
Collections.sort(friendProfiles);
2. You could write a Comparator.
2. 你可以写一个比较器。
public class FriendProfileComparator implements Comparator<FriendProfile>
{
public int compare(FriendProfile fp1, FriendProfile fp2)
{
// Descending order
return fp2.userPosition - fp1.userPosition;
}
}
...
Collections.sort(friendProfiles, new FriendProfileComparator());
When comparing objects rather than primitives note that you can delegate on to the wrapper objects compareTo. e.g. return fp2.userPosition.compareTo(fp1.userPosition)
当比较对象而不是基元时,请注意您可以委托给包装器对象 compareTo。例如return fp2.userPosition.compareTo(fp1.userPosition)
The first one is useful if the object has a natural order that you want to implement. Such as Integer implements for numeric order, String implements for alphabetical. The second is useful if you want different orders under different circumstances.
如果对象具有您想要实现的自然顺序,则第一个很有用。如Integer实现数字顺序,String实现字母顺序。如果您希望在不同情况下使用不同的顺序,则第二个很有用。
If you write a Comparator then you need to consider where to put it. Since it has no state you could write it as a Singleton, or a static method of FriendProfile.
如果你写一个比较器,那么你需要考虑把它放在哪里。由于它没有状态,您可以将其编写为单例或 FriendProfile 的静态方法。
回答by Kumar Vivek Mitra
You can use
java.lang.Comparable interface if you want to sort in only One way.
You can use
java.lang.Comparable 接口,如果你只想以一种方式排序。
But if you want to sort
in more than one way, use java.util.Compartor interface.
But if you want to sort
不止一种方式,使用 java.util.Compartor 接口。
eg:
例如:
The class whose objects are to be Sorted on its roll_nos
对象将在其 roll_nos 上排序的类
public class Timet {
String name;
int roll_no;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getN() {
return roll_no;
}
public void setN(int n) {
this.roll_no = n;
}
public Timet(String name, int n) {
this.name = name;
this.roll_no = n;
}
public String toString(){
return this.getName();
}
}
The class for sorting:
排序类:
public class SortClass {
public void go(){
ArrayList<Timet> arr = new ArrayList<Timet>();
arr.add(new Timet("vivek",5));
arr.add(new Timet("alexander",2));
arr.add(new Timet("catherine",15));
System.out.println("Before Sorting :"+arr);
Collections.sort(arr,new SortImp());
System.out.println("After Sorting :"+arr);
}
class SortImp implements Comparator<Timet>{
@Override
public int compare(Timet t1, Timet t2) {
return new Integer(t1.getN()).compareTo (new Integer((t2.getN())));
}
}
public static void main(String[] args){
SortClass s = new SortClass();
s.go();
}
}