Fix sorted dump and add tests