I intend to sort an array of multidimentional points each time by another coordinate. I want to use c qsort() method but in order to do so I have to use comparator function which its input is only two pointers, so I can't send it the desired coordinate to sort by. Therefore I figured out two solutions and I am struggling choosing the best one of them :
- Use a static variable - an int in this example - initialize it to a -1, and before calling the qsort function set it to the wanted coordinate. In addition, make my comparator, access this variable and compare based on it.
- Build a new struct to hold a pointer to the point and the desired coordinate, then make the comparator to sort two pointers to such struct and use the additional info from the struct.
The first sounds like a quick solution though it might be loop hole, while the second sounds like an overkill for such a simple task. I would be glad to hear any better solution if there is to the problem.