Reputation: 2008
What is the best way to sort the points(first based on x co-ordinate and if x is same then on y co-ordinate and if y is same then based on z co-ordinate and so on.) in java without implementing sorting algorithm?
In c++ it can be done very easily(as follows) with the help of pairs.
For 2D:
Vector < pair < int,int > > plane;
sort(plane.begin(),plane.end())
For 3D:
Vector < pair < int,pair < int,int > > > space;
sort(space.begin(),space.end());
Thanks in Advance. Shantanu
Upvotes: 0
Views: 532
Reputation: 33544
There are few options in Java.
Collections.sort(List l)
Use java.lang.Comparable // For sorting only on the basis of one property
Collections.sort(List l, Comparator c)
Use java.util.Comparator // For sorting in more than one way
If Uniqueness is needed, along with Sorting use TreeSet()
TreeSet() // Sorting in Natural order
TreeSet(Comparator c) // Sorting in more than one way.
Upvotes: 1
Reputation: 272657
You don't need to implement a sorting algorithm. You just need to implement a comparator, which can then be used with Collections.sort()
.
See Object Ordering from the Java Tutorial for more information.
Upvotes: 5