We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
I'm trying to sort an array of vectors v(x,y) based on their index x. I have seen implementations of this in processing (https://forum.processing.org/two/discussion/12141/how-to-sort-an-array-with-index-from-another-array) or javascript (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort). But I'm using P5 and I'm not quite sure how to do this with it. (I know it's the processing forum, but p5 website sent me here).
Is there a way I can do it directly with P5? Or can I integrate pure javascript language in P5 (at the moment I don't seem to be able to use the syntax shown in the javascript reference)?
Cheers, L.
Answers
http://p5ide.HerokuApp.com/editor#?sketch=5751b1d4591f900300aaf9de
https://GitHub.com/therewasaguy/p5js-webIDE/issues/120
@laurage: the key to GoToLoop's answer is that the Array.sort method - which you already linked to - accepts a compare function which allows you to customise the sorting based on the target object. So the code that's most relevant is the function being passed to Array.sort:
Where a and b are the two values being compared from the array; and the return value is the comparison you want to sort on. By adding the OR conditions if a.x === b.x it will move on to the next axis...
Remember that underneath p5js is JavaScript so all the examples on MDN should work even with p5js built in objects...