Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

New function tolerantSort()

kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 559
edited May 2021 in FeatureScript
Hi all,

The latest release (Onshape 1.129 and FeatureScript 1483) came with a new helpful function in the Standard Library: tolerantSort().

This function is useful for sorting items based on measurements that have some inherent imprecision, like sorting a list of lines by length. This function sorts based on those measurements, except that when measurements are within the provided tolerance of each other, it defers to their order in the original list.

This can provide stability to your features, preventing the sort order of two items of equal size from occasionally switching due to unrelated changes.

Documentation for the function and simple example code can be found here.

One Feature Studio which has already been updated to utilize this function is @lemon1324's Laser Joint Utils, used by FeatureScript laser joint. The feature sorted parts from smallest to largest so it could consider the smaller part the "tab" and the larger part the "base". Using tolerantSort ensures that ties in size are broken by the original selection order, not arbitrarily.

Thanks for updating that script Arul, and happy FeatureScripting all!

– Kevin


  • MichaelPascoeMichaelPascoe Member Posts: 1,482 PRO

    Learn more about the Gospel of Christ  ( Here )

    🅲🅰🅳🆂🅷🅰🆁🅿  -  We make custom features and integrated Onshape apps!   CADSharp.com
  • konstantin_shiriazdanovkonstantin_shiriazdanov Member Posts: 1,221 ✭✭✭✭✭
    wish a sort() function also worked on a sampling function which simply returns numeric value instead of boolean of comparison
  • lemon1324lemon1324 Member, Developers Posts: 217 EDU
    @konstantin_shiriazdanov yeah, I ended up writing my own utility function to do that, essentially, but tolerantSort acts on a function returning a value, so in that sense, it does that.  Defining the function inline can still be a bit of a mess to read occasionally, but it's definitely more readable than defining my own function to return a boolean.

    Incidentally, if anyone wants to get a hold of me on the forums, my personal Onshape account @lemon1324 is a bit more likely to actually ping me, rather than my work account - I'm not sure I have forum notifications enable there, and I definitely check the forum with that account less often.
    Arul Suresh
    PhD, Mechanical Engineering, Stanford University
Sign In or Register to comment.