I want to sort a vector

Hi I want to sort the x,y,z vector…


variable.sort();–>it is sort only x value. But I want sort x,y,z…it is possible…

1 Like

the best way to sort normalized vector is convert that to the angels ( alpha and beta)

v = v.normalize();

alpha = atan( vx./v.y ) *180./3.1415265;
beta = atan( vx./v.z ) *180./3.1415265;

sortValue = floor(alpha * 10.) * 100000.+floor(beta * 10.);

if you talk about length of vector
then you can sort them by length
sqrt( pow(v.x,2.)+pow(v.y,2.)+pow(v.z,2.))

2 Likes

Thank you so much… :smiling_face_with_three_hearts:

What is the reason you want them sorted? This will determine the sort method. Think of old fashioned phone books they were grouped by area, in each book entries were in name order. However there would have been times when it would have been useful for the vectors (area, name, number) to have been sorted by number, then area then name.

@nasimiasl has given two ways to sort the vectors by mapping them to a single number first. There are many ways to do the sort depending on what you need.

2 Likes
ArrayOfVectors.sort((a, b)=>{
return a.length() - b.length()
})

??

Or if you need them to be sorted x then y then z

ArrayOfVectors.sort((a, b)=>{
return a.x - b.x
}).sort((a, b)=>{
return a.y - b.y
}).sort((a, b)=>{
return a.z - b.z
})

There is a ton of ways to sort in a custom manor just need to know what your final goal is saying you want to sort by x,y,z is rather abstract.

1 Like

not to be pedantic, but javascript sort is unstable and I wouldn’t want somebody to use that expecting it would work. Chaining built-in javascript sorting will not work that way - instead I think this would do what is described:

vectorArray.sort((a, b) => {
  if (a.x !== b.x) {
    return a.x - b.x;
  }
  if (a.y !== b.y) {
    return a.y - b.y
  }
  return a.z = b.z;
})

if you take this as input you can see the difference:

[{
  x: 5,
  y: 2,
  z: 3
}, {
  x: 3,
  y: 1,
  z: 4
}, {
  x: 3,
  y: 3,
  z: 7
}]

At least on chromium you would see chained sorting is not sorted in the expected manner. Sorting algorithms are typically not stable and do not preserve order.

edit: I only know this because I have made the same mistake before!

image
Curve creating zigzag Because the vector opposite opposite direction…Want to arrange vector. This is my need @JohnK

Please explain what curve you are trying to draw and how you are creating the points on the curve.

I creating a catmullromspline curve…the curve points are getting from scanned a object @JohnK

The points need to be the same order as they are on the scanned object. This depends on your scan order. Impossible to give a solution without knowing what you are scanning, how you are scanning and collecting the data.

1 Like

Hahah thank you for this. ‘“I want to sort” is not enough to fully define the task.’ <3

1 Like

Hi @Jenifer just checking in, do you still need any help?

I changed another platform…Thankyou @carolhmj

If you don’t mind me asking, was there any specific reason you changed the platform? Anything you tried to do and wasn’t able to?

Actually we thing python is more easy so we changed @carolhmj

1 Like