【问题标题】:Sorting an ArrayList of Objects alphabetically按字母顺序对对象的 ArrayList 进行排序
【发布时间】:2013-10-28 13:22:13
【问题描述】:

我必须创建一个方法,根据电子邮件按字母顺序对对象的 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");

}

【问题讨论】:

标签: java sorting arraylist


【解决方案1】:

排序部分可以通过实现自定义Comparator&lt;Vehicle&gt; 来完成。

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    【解决方案3】:

    在此链接中,您可以找到帮助您按降序和升序对对象数组列表进行排序的代码。

    http://beginnersbook.com/2013/12/java-arraylist-of-object-sort-example-comparable-and-comparator/

    【讨论】:

    • 您为已接受答案且更好的问题添加了信息量较少的答案
    【解决方案4】:

    包 srikanthdukuntla;

    导入 java.util.ArrayList; 导入 java.util.List;

    公共类 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);
    
    }
    

    }

    【讨论】:

      【解决方案5】:

      最清晰

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

      【讨论】:

      • 你能再解释一下你的答案吗?
      猜你喜欢
      • 2017-05-22
      • 2019-02-03
      • 2019-08-02
      • 1970-01-01
      • 2021-03-20
      • 2012-11-03
      • 2019-06-15
      • 2013-09-09
      • 1970-01-01
      相关资源
      最近更新 更多