Maybe a hash is not the best data structure to solve your problem.
I would use a binary tree or a B-tree, on the grounds that inserting is relatively cheap, and the inserted items are already sorted. Finding the first N items in sorted order would just be a simple matter of walking down the tree while keeping a count. When the count is over, you stop listing.
Now, what tool provides such mechanism? A DBMS for sure. You insert your values in a table with an index on the columns you need to keep in order, and you get your list faster than you could possibly do with some hard coded algorithms in your script.
Using a database, your sorted list would be just a combination of ORDER BY and LIMIT (choose a DBMS that supports such feature).
You would pay with slower insertion (inserting records into a table is slower than using a hash, but you would minimize the retrieval time, since sorting (by index) is already done.)
BTW, if merlyn were here, he would say that he has a column on this subject (or maybe two).
In reply to Re: *Fastest* way to print a hash sorted by value
by cchampion
in thread *Fastest* way to print a hash sorted by value
by smellysocks
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |