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.
For getting unique elements for your custom class you can just add all elements to something like `HashSet`. For using this you need to override the `Object#hashCode()` method to let it return the same `hashCode()` value `x` and `y` of `MyObject` instance. Don't forget to override `Object#equals()` as well.

You can refer [here](https://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java) for more on overriding `hashCode()` and `equals()`.