# mesh.alphaIndex not working anymore when using scene.setRenderingOrder

**URL:** <https://forum.babylonjs.com/t/mesh-alphaindex-not-working-anymore-when-using-scene-setrenderingorder/50502>\
**Category:** Questions\
**Created:** [May 13, 2024, 10:09am UTC](https://forum.babylonjs.com/t/mesh-alphaindex-not-working-anymore-when-using-scene-setrenderingorder/50502 "2024-05-13T10:09:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joe\_Kerr](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/joe_kerr/32/39017_2.png) [@Joe\_Kerr](https://forum.babylonjs.com/u/Joe_Kerr)\
**Post date:** [May 13, 2024, 10:09am UTC](https://forum.babylonjs.com/t/mesh-alphaindex-not-working-anymore-when-using-scene-setrenderingorder/50502/1 "2024-05-13T10:09:35Z")

</div>

Hi everyone,

Please see this playground: [https://playground.babylonjs.com/#AC8XPN#135](https://playground.babylonjs.com/#AC8XPN#135)

To reproduce: Click the wall, wait for red confirm flash, then click the wall again.

The display order of the decals is reversed. It only happens when I use scene.setRenderingOrder (L17).

Also note: there needs to be added and disposed another mesh in between clicks (L76).

I think the main problem is that I do not really know what the sort function does. I copy and pasted it in order to make occlusion queries work. As I understand it, when the sort function returns 0, it should not reorder. And since, in the playground, it always returns 0, it always should not mess with render order. But it does?

Best wishes  
Joe

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [May 13, 2024, 3:29pm UTC](https://forum.babylonjs.com/t/mesh-alphaindex-not-working-anymore-when-using-scene-setrenderingorder/50502/2 "2024-05-13T15:29:13Z")

</div>

Your sort function is not a sort, it always return 0. You should implement a real sort function, that compares the two elements passed to the function and returns -1, 0 or 1 depending on the ordering of the two elements. This function is passed to the javascript `sort` function, more explanation here:

> **[Array.prototype.sort() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)**
>
> The sort() method of Array instances sorts the elements of an array in place and returns the reference to the same array, now sorted. The default sort order is ascending, built upon converting the elements into strings, then comparing their sequences...

You generally don’t need to overwrite the default sorting functions, so I’m not sure why you have to do it in the first place?

> [@Joe\_Kerr](#):
>
> As I understand it, when the sort function returns 0, it should not reorder.

No, there’s always a sort performed by the rendering manager, and the sort function should return -1, 0 or 1, as explained above.

---

<div class="post-metadata">

**Author:** ![Joe\_Kerr](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/joe_kerr/32/39017_2.png) [@Joe\_Kerr](https://forum.babylonjs.com/u/Joe_Kerr)\
**Post date:** [May 14, 2024, 7:44am UTC](https://forum.babylonjs.com/t/mesh-alphaindex-not-working-anymore-when-using-scene-setrenderingorder/50502/3 "2024-05-14T07:44:30Z")

</div>

Thanks for the clarification @Evgeni_Popov

I removed the sort function and it does not seem to break anything else anymore.
