Describe the bug
Any values added to a SET/ZSET/LIST seem to be sorted by default in ascending order, and totally ignore the order it was added in:
From the simple example on the website>
redis> DEL myset
(integer) 1
redis> SADD myset World
(integer) 1
redis> SADD myset Hello
(integer) 1
redis> smembers myset
1) "Hello" 2) "World"
redis> sort myset by nosort
1) "Hello" 2) "World"
Expected behavior is to see 1) World 2) Hello -
As this was how it was added
It should be in the order it was added and this is the same for ZADD, LPUSH, etc. it seems once the keys are stored it is sorted in its own ascending order.
127.0.0.1:6379> SADD testsort a 2 b 3 c 1 (integer) 6 127.0.0.1:6379> smembers testsort 1) "2" 2) "a" 3) "c" 4) "1" 5) "b" 6) "3"
127.0.0.1:6379> LPUSH testpush a 2 b 3 c 1 (integer) 6
127.0.0.1:6379> lrange testpush 0 5 1) "1" 2) "c" 3) "3" 4) "b" 5) "2" 6) "a"
127.0.0.1:6379> sort testpush by nosort 1) "1" 2) "c" 3) "3" 4) "b" 5) "2" 6) "a"
This seems to keep the order, but in reverse.
Using latest redis 6.2.1 (from Docker if relevant)
Comment From: oranagra
Hash, and Set types don't intend to keep any order, they're stored in a hash table using a random hash seed, so on each run of Redis they may be presented out in a different order which is not lexicographic, nor the order in which they where added.
List keeps order, but since you used LPUSH (left push), the LRANGE command returns them in reverse to the order you pushed them.
Zset, is saved similarly to Set, but it also has a ziplist for order. So it depends on how you query it, a simple ZRANGE will give them sorted by the rank (not the order you added them). If you use score of 0 for all items, then they are sorted lexicographically.
Comment From: gittyup2018
I didn't know sets were stored as hashes and yes, RPUSH seems to keep the correct order. I guess I'm a little rusty using/thinking in Redis terms, sorry for any inconvenience.