I wish to sort my objects in order of their Email address.
This is the method I've attempted but it does not work, but I'm not even sure it's the correct way to do what I want?
public static ArrayList<Billing> sortedListByEmail(ArrayList<Billing> Billing) {
ArrayList<Billing> Sort = new ArrayList<Billing>();
for (int i = 0; i < Sort.size(); i++) {
Collections.sort(Sort, new Comparator<Billing>() {
public int compare(Billing o1, Billing o2) {
return o1.getEmail() > o2.getEmail() ? -1 : o1.getEmail().equals(o2.getEmail() ? 0 : 1);
}
});
}
return Sort;
}
Rest of the class:
import java.util.ArrayList;
import java.util.Collections;
import java.lang.Comparable;
import java.util.Comparator;
public class Billing extends User implements Comparable<User> {
private Address billingAddress;
private String email;
public Billing(String id, String firstName, String lastName, String userName, String password, UserType userType, PermissionType permission, Boolean Status, Address billingAddress, String email) {
super(id, firstName, lastName, userName, password, userType, permission, Status);
this.billingAddress = billingAddress;
this.email = email;
}
public Billing(String id, String firstName, String lastName, String userName, String password, UserType userType, PermissionType permission, Boolean Status, String email) {
super(id, firstName, lastName, userName, password, userType, permission, Status);
}
public Billing(String id, String firstName, String lastName, String userName, String password, UserType userType, PermissionType permission, Boolean Status) {
super(id, firstName, lastName, userName, password, userType, permission, Status);
}
public Address getBillingAddress() {
return billingAddress;
}
public void setBillingAddress(Address billingAddress) {
this.billingAddress = billingAddress;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
How would I accomplish sorting the objects in order of their email address? Thank you
This will change the existing array, no need of new variable,
try the below code :
Since the
email
is aString
and also aComparable
you can use theString.compareTo
to sort using theemail
property. (This is a pre Java-8 solution):In Java-8 this can be made more compact, with the
Comparator#comparing
:Here the original list supplied is not sorted in place, instead a copy is made which is sorted.
Or you can use
Stream#sorted
to do it in an elegant way which will also return you a newList
instead of modifying the original one:With considering that Billing List object name is
billingList
1) If you are using Java 8 you can do it using below approaches:
Using Collections.sort with Lambda expression:
Using sort method of List Interface (default method)
2) If you are using earlier version of Java
If you want to do it old ways then got for below code
Few things you are doing wrong in your code - First -
return o1.getEmail() > o2.getEmail() ? -1 : o1.getEmail().equals(o2.getEmail() ? 0 : 1);
you cannot compare a string value like integer value.Second - You are creating an empty array list and sorting it. It has no elements, it is empty.
ArrayList<Billing> Sort = new ArrayList<Billing>(); ........ Collections.sort(Sort, new Comparator<Billing>() {
With java 8 features it can be done in very less line of code as below