The IRS is well aware that many owners of S corporations are tempted to underreport salary to avoid paying payroll taxes, while taking a hefty payroll-tax-free dividend.
These ordering rules are intended to be taxpayer-friendly, as S corporation shareholders would generally prefer to have a distribution treated as a tax-free return of basis than a taxable dividend.
So if trust income is a mixture of dividend, capital gain and tax-free income (from muni bonds or principal), the ordinary income must be used up before the payout can be taxed as gain, and gain must be used before the payout can be tax-free.