You should checkout srandrd, it lets you run a simple bash script when monitor changes happen. It includes some Env vars so you can figure out what happened.
Been a while but I had real issues using udev rules as they created timing problems or something like that.
It's pretty telling how the top two solutions here for this common problem involve writing custom scripts. I love scripting at least as much as the next guy, but this seems like something that should be easily configurable out of the box without a text editor.
If your setup is relatively static, then there are GUI tools that will work just fine. But if you have displays coming and going in different orders, it's quite a big ask to have a GUI tool that can define the sort of conditional logic that is required to get things right. This is precisely what I've got in my case.
It does for me (fedora, gnome, wayland). Other stacks are more work. I was put off using Sway as my desktop, because screen resolutions and layouts was manual.
It gets better. One of my ASUS monitors has a defect (it kicked in after several years of use) where it no longer provides EDID information. I believe that on windows and macOS this would effectively be the end-of-life for the monitor, which is a shame since in every other way it is still 100% functional. On Linux, I was able to tell the kernel to use a file for the EDID information, and created the file using the other (identical) monitor.
Very much a corner case, but also another perfectly good monitor kept out of a landfill.
Windows may actually let you override the EDID info, it's been a while since I used windows but I think it lets you do it in device manager... from the era when displays came with drivers on a cdrom.
There are also EDID cloner/emulator hardware solutions that you plug in inline on the display cable.
My linux machine remembers the displays and where I place them relative to each other (I'm not using a laptop).
But ... I sometimes use 1, 2 or 3 of the displays, and the configuration needs to be different depending on the number and specific displays in use.
My Linux machine can do this automatically, because this is scripted. Maybe you can do this on macOS also, but I doubt it can be done with nothing but the GUI tools.
ime, this isn't totally accurate in the world of usb-c HDMI dongles. I've had the same 2 monitors plugged into the same 2 ports since the machine went to sleep, and it will occasionally switch their positions upon waking.
Been a while but I had real issues using udev rules as they created timing problems or something like that.
https://github.com/jceb/srandrd