const result = [...'Hello World Foo Bar'] .sort((a, b) => /[A-Z]/.test(a) ? /[A-Z]/.test(b) ? 0 : -1 : 0) .join(''); return result;