Sort collections

Another try to fix #2328. The tests are coming.

Twin: #2340