TikTok interview question

Write code to implenment the Sort the flags problem/