We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I was trying to sort an array of object [] people, within each people object, I have 2 parameters, height and weight. I use JAVA script to sort height, using
int compareTo(Object objI)
{
Individual iToCompare = (Individual) objI; //cast object to individual
if(height < iToCompare.height)
{
return -1;
}
else if(height > iToCompare.height)
{
return 1;
}
return 0;
}
and then use
Arrays.sort(pop);
to sort my people according to height, but I also want another sort according to weight, how should I do that....?
Answers
https://forum.Processing.org/two/discussions/tagged?Tag=sort()
https://forum.processing.org/two/search?Search=comparable
https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html Check the section Integrating Default Methods into Existing Libraries.
Kf
If your class implements
Comparable
then you have to add thecompareTo
method which provides natural ordering. For instance the words in a dictionary have a natural ordering and numbers have a natural ordering but a particular type (class) can only have one natural ordering.The solution is to create a special class that implements the Comparator interface. This classes will contain a single method to compare two objects in a similar way to
compareTo
. An object of the class is used by the Arrays.sort method to order the objects in the array.The advantage of this approach is that we can have as many comparators with different implementations used to compare objects.
The code below produced this output and demonstrates the use of comparators.
@quark thank you sooo much for your detailed explanation, I hardly know anything about java and know that I should have learnt it from textbook but there is little time for me.. I worked it out as your statements and it works pretty good! Thanks so much.
Also many thanks to @GoToLoop and @kfrajer, I had looked through the forum as well as the java website and its very helpful for further self study!
@quark sorry for disturbing you again,,, but I was wondering is there a way to translate my object according to the sequence in two arrays I've got, for example, person A is the heaviest and the third tallest, so I want to
translate (A's sequence in array[0], A's sequence in array [1]);
I searched but not much results..
I am afraid I have no idea what you mean.
Assuming that their are 10 people in the array, in other words in elements 0-9 incl. then -
If person A is the heaviest person and the array is sorted by weight then they will be in element [9].
If person A is the third tallest person and the array is sorted by height then they will be in the element [7].
What do you mean by translate?
You can have more than one array holding the same date like this
@quark sorry for my confusing description..
then I'd like to translate A to (9interval,7interval);
9 * interval, 7 * interval.
don't know why the multiplication sign disappeared.
Still doesn't make sense what is 'interval'?
Are you taking about a 2D array?
@quark
No..I'm talking about 2 arrays, sorted using the java class you've taught me. interval is just any integer, maybe 10...the important part is that it will be translated (9,7)
I'll explain again..Since I have sort an array twice, for each individual, I now have 2 index for it, and I want to know the two index.
@vapeur -- an alternate approach:
Now regardless of future sorting or random lookups you can retrieve these values from any row you are accessing using getFloat() https://processing.org/reference/Table_getFloat_.html
@jeremydouglass thanks! I am working on it. but how to "record" the sorted index in each height index? not sure I'm doing the right thing...
I can't test that code at the moment without a minimal complete example (and you should format your code in that post) however that does look correct.
When you are done you can use saveTable to inspect your data according to either sorting.
When you say translate, are you using their rankings to position some person graphic on the screen? For instance a persons x position is controlled by their height and their y position is controlled by their weight.
@jeremydouglass Thanks! I have no idea why it does not present as the code form, but I've already set it.. However, taken your advice it works totally fine! Thanks so much!
@quark yes, I am using the ranking for the position of each person, showing on the screen. Though I made it with table class, I'd like to know other approach when you have time~
OK here is the output from the sketch code below. Notice that person A is the shortest and person E the heaviest.
Thanks for the getrank function! is simple and great! :D