Sort an ArrayList of objects?

后端 未结 7 1685
轻奢々
轻奢々 2021-01-29 01:47

I need some help how to sort an ArrayList of objects. I have the superclass Account and two subclasses SavingsAccount and CreditAccount. Inside the Account class I have this met

7条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-29 02:09

    java.util.Collections.sort(list) - use it when Account implements Comparable.

    java.util.Collections.sort(list, new Comparator(){
         public int compare(Account a,  Account b){
              return a.getAccountNumber().compareTo(b.getAccountNumber()
         }
    });
    

提交回复
热议问题