Sorted collection

Fixes #1061

Think my tests cover most cases, please let me know if there are any edge case i haven't considered.