sort an array by another array?

EvanReese
EvanReese Member, Mentor Posts: 3,105 PRO
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
The Onsherpa | Reach peak Onshape productivity
www.theonsherpa.com

Best Answers

  • MBartlett21
    MBartlett21 Member, OS Professional, Developers Posts: 2,064 ✭✭✭✭✭
    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
  • MBartlett21
    MBartlett21 Member, OS Professional, Developers Posts: 2,064 ✭✭✭✭✭
    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

  • MBartlett21
    MBartlett21 Member, OS Professional, Developers Posts: 2,064 ✭✭✭✭✭
    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
  • EvanReese
    EvanReese Member, Mentor Posts: 3,105 PRO
    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
    The Onsherpa | Reach peak Onshape productivity
    www.theonsherpa.com
  • MBartlett21
    MBartlett21 Member, OS Professional, Developers Posts: 2,064 ✭✭✭✭✭
    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