按字母顺序对对象的ArrayList进行排序

30

我需要创建一个方法,按照邮件地址的字母顺序对对象的ArrayList进行排序,然后打印排序后的数组。我的问题是排序部分。我已经进行了研究,并尝试使用Collections.sort(vehiclearray);,但这对我没有起作用。我被告知需要使用一个叫做比较器的东西,但我无法弄清楚它的工作原理。我是否必须使用它们,还是可以使用冒泡排序或插入排序之类的方法来完成这种操作?

以下是我目前拥有的代码:

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");

}

3
不,你需要一个比较器。关于它有什么问题? - Jeroen Vannevel
2
请遵循Java命名规范:使用Vehicle代替vehicle。并且可能使用printAllSortvehicleArraySort - arshajii
1
有很多教程可以解释如何为您的类实现比较器。首先尝试检查这个:http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html - Alexis C.
@arshajii是正确的。请阅读此文:Java编程语言的代码约定 - informatik01
5个回答

97

通过实现自定义的 Comparator<Vehicle>,可以完成排序部分。

Collections.sort(vehiclearray, new Comparator<Vehicle>() {
    public int compare(Vehicle v1, Vehicle v2) {
        return v1.getEmail().compareTo(v2.getEmail());
    }
});

这个匿名类将用于按照其对应电子邮件的字母顺序对ArrayList中的 Vehicle 对象进行排序。

升级到Java8后,您还可以使用方法引用以更简洁的方式实现此操作:

Collections.sort(vehiclearray, Comparator.comparing(Vehicle::getEmail));

14

虽然这个问题已经有了一个被接受的答案,但我想分享一些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));

2
我认为可以使用Comparator.comparing来简化这个问题:https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#comparing-java.util.function.Function- - Philipp Wendler

0

你给一个已经有被接受且更好的回答的问题添加了一个信息较少的回答。 - shikjohari

0

包 srikanthdukuntla;

import java.util.ArrayList; import 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);

}

}


0

最清晰的

vehiclearray.sort(Comparator.comparing(Vehicle::getEmail()));

你能详细解释一下你的答案吗? - RamenChef

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接