How to sort, store and POP weighted items using ZADD and ZPOPMIN|MAX in redis?
There is often a need to sort items(strings, integers etc.) on the basis of weight attached along with each one of them. Though a SORT by WEIGHT feature is available in redis, as found at https://redis.io/commands/sort , another way to dynamically sort and store weighted items is explained here. For example consider the following items(elements) along with their weights:- Set name = ordered_array In order to get these elements in the order of their weights, add them to the redis data structure using ZADD and pop each element one by one using ZPOPMIN (or ZPOPMAX if needed in opposite order of weights). A running redis-server is required for this code to work. For installation instructions on redis refer to https://foolsvilla.blogspot.com/2020/03/installing-redis-508-on-linux-ubuntu.html . The hiredis library is installed along with redis. Following C code is used along with this hiredis library to access redis data structure. The code adds elements with weights to redis and