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
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.