Rotate mesh to best fit based on bounding box

I need to automatically rotate a mesh so the shortest side is facing the World Y axis and the longest side is along the World X axis.

Update: I have used an array to convert XYZ to intergers, fill a list by order of length, feed my list to a map which will determine final rotation, it’s a manual setup. I am still planning to create a more automatic version but will use this for now
Here is my graph
If someone has a better solution, please post it, I would appreciate it alot!