Java 按字母顺序对对象的 ArrayList 进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19471005/
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 an ArrayList of Objects alphabetically
提问by Fluxxxy
I have to create a method that sorts an ArrayListof objects alphabetically according to email and then prints the sorted array. The part that I am having trouble with it sorting it. I have researched it and tried using Collections.sort(vehiclearray);
but that didn't work for me. I was that I needed something called a comparator but couldn't figure out how it worked. Will I have to use those or can something like bubble sort or insertion sort work for this kind of thing?
我必须创建一个方法,根据电子邮件按字母顺序对对象的ArrayList进行排序,然后打印排序后的数组。我在整理它时遇到问题的部分。我已经研究过它并尝试使用,Collections.sort(vehiclearray);
但这对我不起作用。我是我需要一个叫做比较器的东西,但不知道它是如何工作的。我是否必须使用它们,或者诸如冒泡排序或插入排序之类的东西可以用于这种事情吗?
This is the code that I have so far:
这是我到目前为止的代码:
public static void printallsort(ArrayList<vehicle> vehiclearray){
ArrayList<vehicle> vehiclearraysort = new ArrayList<vehicle>();
vehiclearraysort.addAll(vehiclearray);
//Sort
for(int i = 0; i < vehiclearraysort.size(); i++)
if ( vehiclearray.get(i).getEmail() > vehiclearray.get(i+1).getEmail())
//Printing
for(i = 0; i < vehiclearraysort.size(); i++)
System.out.println( vehiclearraysort.get(i).toString() + "\n");
}
采纳答案by Konstantin Yovkov
The sorting part can be done by implementing a custom Comparator<Vehicle>
.
排序部分可以通过实现自定义Comparator<Vehicle>
.
Collections.sort(vehiclearray, new Comparator<Vehicle>() {
public int compare(Vehicle v1, Vehicle v2) {
return v1.getEmail().compareTo(v2.getEmail());
}
});
This anonymous class will be used for sorting the Vehicle
objects in the ArrayList
on the base of their corresponding emails alphabetically.
这个匿名类将用于按字母顺序根据相应电子邮件的基础对 中的Vehicle
对象进行ArrayList
排序。
Upgrading to Java8 will let you also implement this in a less verbose manner with a method reference:
升级到 Java8 还可以让您使用方法引用以更简洁的方式实现这一点:
Collections.sort(vehiclearray, Comparator.comparing(Vehicle::getEmail));
回答by SubOptimal
Although the question already has an accepted answer I would like to share some Java 8 solution
虽然这个问题已经有一个公认的答案,但我想分享一些 Java 8 解决方案
// if you only want to sort the list of Vehicles on their email address
Collections.sort(list, (p1, p2) -> p1.getEmail().compareTo(p2.getEmail()));
.
.
// sort the Vehicles in a Stream
list.stream().sorted((p1, p2) -> p1.getEmail().compareTo(p2.getEmail()));
.
.
// sort and print with a Stream in one go
list.stream().sorted((p1, p2) -> p1.getEmail().compareTo(p2.getEmail())).forEach(p -> System.out.printf("%s%n", p));
.
.
// sort with an Comparator (thanks @Philipp)
// for the list
Collections.sort(list, Comparator.comparing(Vehicle::getEmail));
// for the Stream
list.stream().sorted(Comparator.comparing(Vehicle::getEmail)).forEach(p -> System.out.printf("%s%n", p));
回答by Lincy Laiju
In this link you can find code which will help you to sort arraylist of objects in descending and ascending order.
在此链接中,您可以找到帮助您按降序和升序对对象数组列表进行排序的代码。
http://beginnersbook.com/2013/12/java-arraylist-of-object-sort-example-comparable-and-comparator/
http://beginnersbook.com/2013/12/java-arraylist-of-object-sort-example-comparable-and-comarator/
回答by Srikanth Dukuntla
package srikanthdukuntla;
包 srikanthdukuntla;
import java.util.ArrayList; import java.util.List;
导入 java.util.ArrayList; 导入 java.util.List;
public class AlphabetsOrder {
公共类字母顺序{
public static void main(String[] args) {
String temp;
List<String> str= new ArrayList<String>();
str.add("Apple");
str.add("zebra");
str.add("Umberalla");
str.add("banana");
str.add("oxo");
str.add("dakuntla");
str.add("srikanthdukuntla");
str.add("Dukuntla");
for(int i=0;i<str.size();i++){
for(int j=i+1;j<str.size();j++){
if(str.get(i).compareTo(str.get(j))<0){
temp=str.get(i);
str.set(i, str.get(j));
str.set(j,temp );
}
}
}
System.out.println("List of words in alphabetical order "+str);
}
}
}
回答by Orhan Avan
clearest
最清晰
vehiclearray.sort(Comparator.comparing(Vehicle::getEmail()));