We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have some fish objects, in a 1D array that i need to sort, so that the bigger fish, will be first in the array. I know that i can use sort() to sort numbers, strings and characters.
From c++ i know the function std::sort(), which can sort any array of objects that has a < operator, even user defined calsses.
So to sort my fish in c++, i would only need to add a < operator function, like this:
class fish
{
int size;
...
bool operator<(const fish& that) const{return size<that.size;}
...
}
and then i would be able to use std::sort() on an array, of fish.
But i do still want to make this project in processing.
And in processing, there is no way to overload operators, so what am i then suposed to do, if i want to use sort on a class i have made.
Answers
This is a way to do it.
This is not Processing-specific, but something from Java. So if you ever get stuck at a similar problem, you can search how to do it in Java instead.
Here's the documentation of the Comparable interface: http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html Relevant discussion: http://stackoverflow.com/questions/18441846/how-to-sort-an-arraylist-in-java
java.util.Collections.sort() is for non-array containers.
java.util.Arrays.sort() is what you want for regular arrays.
Some old examples from the forum: O:-)
http://forum.Processing.org/two/discussion/7001/how-to-parse-time
http://forum.Processing.org/two/discussion/8973/sorting-list-of-objects-by-object-variable