can you give me a hint how to sort the points according to x coordinates in O(nLogn)? 

```
    Set<Point> points = new HashSet<Point>();
    points.add(new Point(9, 0));
    points.add(new Point(1, 1));
    points.add(new Point(5, 6));
    points.add(new Point(3, 3));
    points.add(new Point(2, 7));
    points.add(new Point(1, 8));
    points.add(new Point(6, 1));

```

I wish I could do something like `Point[] points2 = points.toArray();` and afterwards sort it. But of course it isn´t working.
['https://Stackoverflow.com/questions/47305875', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/8027142/']
```
Set<Point> sortedPoints = points
                        .stream()                                    
                        .sorted(Comparator.comparing(Point::getX))
                        .collect(Collectors.toCollection(LinkedHashSet::new));

```

I am sure this way is better of all because: 
1) on the big amount of Points you can use 

```
.paralellStream()

```

to increase the performance.

and

2) the notation has declarative style and looks more clear.
Any `Set` that implements `SortedSet` interface can keep elements sorted within it. `TreeSet` is a known implementation of `SortedSet`.

You can pass a custom Comparator for your class in the `TreeSet` constructor in order to obtain a set sorted according to your conditions.

```
public static void main(String[] args) {
    SortedSet<Point> points = new TreeSet<>(new Comparator<Point>() {
        @Override
        public int compare(Point o1, Point o2) {
            return o1.getX()- o2.getX();
        }
    });
    points.add(new Point(9, 0));
    points.add(new Point(1, 1));
    points.add(new Point(5, 6));
    points.add(new Point(3, 3));
    points.add(new Point(2, 7));
    points.add(new Point(1, 8));
    points.add(new Point(6, 1));
}

```

If the comparator code looks verbose to you, you can try the new Java 8 style comparator declaration.

```
SortedSet<Point> points = new TreeSet<>(Comparator.comparing(Point::getX));

```

Regarding the last point, if you want to obtain an array from the `Set`, you can always do that by using following:

```
Point[] points2 = points.toArray(new Point[0]);

```

But, if your sole purpose is sorting the data, this should be avoided.