Sharder struct order by

Hello. I have an unusual task in my shader. After passing my own data
through color variable to shader I’m getting it and writting to the three same structs: MyStruct struct1, MyStruct struct2, MyStruct struct3. What the best way to order it by one of int values inside (f.e int overlay). May be result I can save to the new struct like this:

GeneralStruct
{
MyStruct firstAfterOrder;
MyStruct secondAfterOrder;
MyStruct thirdAfterOrder
};

If it was C# with Linq, I’ll do something like: strutsList.(struct => OrderBy(struct.Overlay) ))))

Honestly the best option is to not sort in the shader.

Otherwise you might use indirection on modern hardware, ie: an array of structs and a separate array of indices so you never have to move the structs. This will be slow on older (mobile) hardware.

Last option is to just have a couple of if functions that do the sort. For 3 options this isn’t too bad.

if (a > b && a > c)
{
// a first
if (b > c)
// b second
else
// c second
}
else if (b > a && b > c)
{
// b first
if (a > c)
// a second
else
// c second
}
else // if (c > a && c > b)
{
// c first
if (a > b)
// a second
else
// b second
}

On mobile all paths are calculated and the unneeded results are thrown out at the end. On desktop it may or may not calculate all paths depending on what the compiler decides to do.