Question: I have a ArrayList of CustomObject, my object is simply made of two values x and y 

```
MyObject = (double)x && (double)y 

```

What I want to do is: 

1. remove duplicate from array : simply remove duplicate object and if the objects have the same x keep the one if the higher y.
2. reorder the list depending on the x value (x is for the time, y is a factor)

I have tried using `Collections.sort` to reorder my array and using a set to remove duplicate but nothing works. 

```
Collections.sort(listPoints, new Comparator<MyObject>(){
    @Override
    public int compare(MyObject lhs, MyObject rhs) {
        return Double.compare(lhs.y, rhs.y);  
    }
});

Collections.sort(listPoints, new Comparator< MyObject >(){
    @Override
    public int compare(MyObject lhs, MyObject rhs) {
        return Double.compare(lhs.x, rhs.x);  
    }
});

```

Answer: 
Sorting a list twice with different comparators will not work. You need to compare both x and y in the same comparator.

```
public int compare(MyObject lhs, MyObject rhs) {
    int result = Double.compare(lhs.y, rhs.y);
    if (result != 0) return result;
    return Double.compare(lhs.x, rhs.x);
}

```

Using a set to remove duplicates will work if MyObject correctly implements the equals() and hashCode() methods.
To remove duplicates and keep the sorted order, i would suggest the following:

* You store the Objects of type MyObject in a List
* Sort them as you are doing with x field
* iterate over the list and check for duplicates with previous elements (implements equals and hashCode methods in MyObject class) and remove one with less value in 'y' field.