The script above was breaking every time anything reloaded and you’d need to click something inside the project folder for it to sort.
So instead i just directly modified UnityEngine.CoreModule.dll using dnSpy:
(Applications/Unity/Hub/Editor/2021.3.9f1/Unity.app/Contents/Managed/UnityEngine/UnityEngine.CoreModule.dll)
(For 2022+ it’s UnityEditor.CoreModule.dll)
UnityEditor → ProjectBrowser → GetShouldShowFoldersFirst() → right click → Edit IL instructions → replace instructions with “ldc.i4.1”, “ret” (meaning ‘return true;’).
File > Save Module, replace the dll. Have windows style sorting forever without any scripts. Works with Unity 2021.3.9f1 for me.