Element sorting

TODO: describe the algorithm of how are collected elements sorted.