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.

sort an array by another array?

EvanReeseEvanReese Member, Mentor Posts: 2,135 ✭✭✭✭✭
I have an array of faces that are in a seemingly random order in the array (by index), but they are arranged linearly enough that I could measure them from a certain point and get a sortable list of values. How do I use the array of distances to re-order the array of faces?

Maybe this question is too specific and there's actually a better approach altogether. If so I'd love to hear it.
Evan Reese

Best Answers

  • MBartlett21MBartlett21 Member, OS Professional, Developers Posts: 2,050 ✭✭✭✭✭
    Answer ✓
    @Evan_Reese

    Did you know that the FeatureScript maps allow the key to be anything? [1]

    Because of this, you can have a map of distances, indexed by the face Query. This makes sorting very simple, just using Onshape's sort function:<pre>sort(arrayOfQueries, function(a, b) { return distances[a] - distances[b] }) </pre>
    [1]: Values and Types
    mb - draftsman - also FS author: View FeatureScripts
    IR for AS/NZS 1100
  • MBartlett21MBartlett21 Member, OS Professional, Developers Posts: 2,050 ✭✭✭✭✭
    Answer ✓
    @Evan_Reese

    Here is an example:

    // This map gets filled out with the keys being `Query`s and the values being `ValueWithUnits`s.
    var queryToDistance is map = {};
    
    var queryArray is array = ...;
    
    for query in queryArray {
      queryToDistance[query] = evDistance(context, ...);
    }
    
    queryArray = sort(queryArray, function(a, b) { return queryToDistance[a] - queryToDistance[b] });


    mb - draftsman - also FS author: View FeatureScripts
    IR for AS/NZS 1100

Answers

  • MBartlett21MBartlett21 Member, OS Professional, Developers Posts: 2,050 ✭✭✭✭✭
    Answer ✓
    @Evan_Reese

    Did you know that the FeatureScript maps allow the key to be anything? [1]

    Because of this, you can have a map of distances, indexed by the face Query. This makes sorting very simple, just using Onshape's sort function:<pre>sort(arrayOfQueries, function(a, b) { return distances[a] - distances[b] }) </pre>
    [1]: Values and Types
    mb - draftsman - also FS author: View FeatureScripts
    IR for AS/NZS 1100
  • EvanReeseEvanReese Member, Mentor Posts: 2,135 ✭✭✭✭✭
    Thanks @MBartlett21 I'm not sure whether you meant make the face query the key or the value. I got it working by making it the value, with the distance as key, but I have a feeling I took the long way. If you have time I'd appreciate a simple example showing what's in each variable above.
    Evan Reese
  • MBartlett21MBartlett21 Member, OS Professional, Developers Posts: 2,050 ✭✭✭✭✭
    Answer ✓
    @Evan_Reese

    Here is an example:

    // This map gets filled out with the keys being `Query`s and the values being `ValueWithUnits`s.
    var queryToDistance is map = {};
    
    var queryArray is array = ...;
    
    for query in queryArray {
      queryToDistance[query] = evDistance(context, ...);
    }
    
    queryArray = sort(queryArray, function(a, b) { return queryToDistance[a] - queryToDistance[b] });


    mb - draftsman - also FS author: View FeatureScripts
    IR for AS/NZS 1100
Sign In or Register to comment.