Reputation: 4638
In a Redis Set (not sorted set). I noticed the values are in the order they are added with the newest being first. I didn't find in the docs if the set will always maintain this order. So my question, can I trust they will always be ordered like this?
Upvotes: 8
Views: 2648
Reputation: 31538
In general, you cannot trust the order of the elements. If you want it in a particular order, you either use a list or a sortedset.
A Set is built using a HashTable. There are absolutely no guarantees to the order of the element.
However, if you are set only contains integers, and the number of elements is less than the directive set-max-intset-entries
(by default 512) - then the set is built on a structure called IntSet, or Integer Set. An Integer Set is a sorted integer array. The iterator for such a set will return elements in ascending order.
So, more specifically, if you can program to an integer set, you can assume the elements will be in sorted order.
Upvotes: 12