If you only want to do "top 10" like extract activity, you can do less work with a `HeapQueue[tuple[count: int; key: K]]`, as in [https://github.com/c-blake/cligen/blob/master/examples/newest.nim](https://github.com/c-blake/cligen/blob/master/examples/newest.nim) (see also the `heapqueue` module).
Using the above you just iterate over key,count in the `CountTable[K]` and retain the top 10 (or `M`) in a heap you can pop out in the correct order (or possibly its reverse) which is `O(N * log(M))` where `N=CountTable.len=` num of distinct keys. Doing the sort and then filter way, as you were requesting, you have the whole `N*log(N)` sort. So, if `log(N)` is much greater than log(M) it will be much slower. Basically rolling your own filtering sort is not hard and faster.