Preserve spaces order on monitor unplug on MacBook
Our questions weren't identical (which is why I went ahead and posted my own question) but I think the answer to mine would help you too: Spaces disappear (but not their application windows) when I unplug external monitors

In brief: you can prevent the behavior by unchecking "Displays have separate Spaces" in System Preferences -> Mission Control, but you may or may not like the new behavior any better. Spaces won't disappear anymore but you'll cycle through Spaces on all monitors at the same time, and when you plug your monitors back in, you may find that all application windows stay in the right Spaces but lose their placement (i.e. if you carefully arranged the application windows to fit nicely next to each other on your screen, you may find the windows all stacked on top of each other when you plug the monitor back in).

Whether or not you choose to uncheck "Displays have separate Spaces," you can use a window management app such as Stay or DisplayMaid to save your window configuration and move the windows back to your preferred arrangment after a change.