i want to sort a NativeArray using Jobs… i’m able to do this with standard Lists but can not make it work using native arrays…
How Sorting should be done ?
BVHTriangle is a struct like this
public struct BVHTriangle
{
public Vector3 v0;
public Vector3 v1;
public Vector3 v2;
public Vector3 centroid;
}
NativeArray<BVHTriangle> tris;
BVHTriangleSort trianglesSort = new BVHTriangleSort();
trianglesSort.SortArray(tris);
public struct BVHTriangleSort : IComparer<BVHTriangle>
{
public void SortArray(NativeArray<BVHTriangle> triangles)
{
triangles.Sort(Compare);
}
public int Compare(BVHTriangle a, BVHTriangle b)
{
if (a.centroid.x - b.centroid.x < 0f)
{
return -1;
}
return 1;
}
}
I tried a lot of things but i always get an error like this:
Your code is almost correct. Working from memory, but if I recall correctly, you need to provide your comparer struct as the argument to Sort(). You can probably get rid of the SortArray method from the struct and just do this:
Hey, this doesn’t seems to help as the only error is actually in the “BVHTriangleSort” struct as visible in the screenshot above ! It’s hard to come up with a working solution as there are no examples i could find !
Hi, thanks for suggestion. I did try implement IComparable in the “BVHTriangle” struct at first, but i still get the same error… I also can’t simply call Sort() as i need a comparer to sort list based on a rule !
I really can’t understand the usage of the Sort method with a comparer, so any other idea is really appreciated !