/*
Returns the rank of member in the sorted set stored at key, with the scores
ordered from high to low. The rank (or index) is 0-based, which means that the
member with the highest score has rank 0.

http://redis.io/commands/zrevrank
*/
167899
func (c *Client) ZRevRank(key string, member interface{}) (int64, error) {
	var ret int64

	err := c.command(
		&ret,
		[]byte("ZREVRANK"),
		[]byte(key),
		to.Bytes(member),
	)

	return ret, err
}
505836
def rank_for_in(self, leaderboard_name, member):
        '''
        
        '''
        if self.order == self.ASC:
            try:
                return self.redis_connection.zrank(
                    leaderboard_name, member) + 1
            except:
                return None
        else:
            try:
                return self.redis_connection.zrevrank(
                    leaderboard_name, member) + 1
            except:
                return None
1286862
func (s VertexSet) rank(n int) int {
	if len(s.set) == 0 || n < s.set[0].a {
		return -1
	}
	// The smallest index i such that n < set[i].a
	i := sort.Search(len(s.set), func(i int) bool {
		return n < s.set[i].a
	})
	in := s.set[i-1]
	if n >= in.b {
		return -1
	}
	return in.index + n - in.a
}
791867
func (r *Redis) ZRevRank(key, member string) (int64, error) {
	rp, err := r.ExecuteCommand("ZREVRANK", key, member)
	if err != nil {
		return -1, err
	}
	if rp.Type == ErrorReply {
		return -1, errors.New(rp.Error)
	}
	if rp.Type == IntegerReply {
		return rp.Integer, nil
	}
	if rp.Type == BulkReply {
		return -1, nil
	}
	return -1, errors.New("ZREVRANK reply protocol error")
}
962233
func (eng *engine) IndexRank(index Index, key interface{}) (int64, error) {
	s := eng.newSession()
	defer s.Close()
	action, rank, err := s.indexRank(index, key)
	if err != nil {
		return rank, s.catch("IndexRank: "+action, err)
	}
	return rank, nil
}
894792
func MapGetByRankRangeCountOp(binName string, rank int, count int, returnType mapReturnType) *Operation {
	return newCDTCreateOperationIndexCount(_CDT_MAP_GET_BY_RANK_RANGE, _MAP_READ, binName, rank, count, returnType)
}
553121
func (s *ScoreSort) Source() (interface{}, error) {
	source := make(map[string]interface{})
	x := make(map[string]interface{})
	source["_score"] = x
	if s.ascending {
		x["order"] = "asc"
	} else {
		x["order"] = "desc"
	}
	return source, nil
}
834334
func (db *RedisDB) ZRem(k, member string) (bool, error) {
	db.master.Lock()
	defer db.master.Unlock()
	if !db.exists(k) {
		return false, ErrKeyNotFound
	}
	if db.t(k) != "zset" {
		return false, ErrWrongType
	}
	return db.ssetRem(k, member), nil
}
506497
func (t *PackageList) Sorted() []*build.Package {
	t.ensureInit()

	keys := make([]string, len(t.dirpool))
	i := 0
	for k := range t.dirpool {
		keys[i] = k
		i++
	}
	sort.Strings(keys)

	result := make([]*build.Package, len(t.dirpool))
	i = 0
	for _, k := range keys {
		result[i] = t.dirpool[k]
		i++
	}

	return result
}
667897
func (db *RedisDB) Members(k string) ([]string, error) {
	db.master.Lock()
	defer db.master.Unlock()
	if !db.exists(k) {
		return nil, ErrKeyNotFound
	}
	if db.t(k) != "set" {
		return nil, ErrWrongType
	}
	return db.setMembers(k), nil
}
906922
func (r *Redis) Sort(key string) *SortCommand {
	return &SortCommand{redis: r, key: key}
}
357403
func (db *RedisDB) ssetScore(key, member string) float64 {
	ss := db.sortedsetKeys[key]
	return ss[member]
}
284324
func (db *RedisDB) ssetRem(key, member string) bool {
	ss := db.sortedsetKeys[key]
	_, ok := ss[member]
	delete(ss, member)
	if len(ss) == 0 {
		// Delete key on removal of last member
		db.del(key, true)
	}
	return ok
}
665297
func (n *Node) MemberStats() MemberStats {
	members := members(n.memberlist.GetMembers())
	sort.Sort(&members)
	return MemberStats{n.memberlist.Checksum(), members}
}
1137054
func (e *Entry) GetIndex(key string) int {
	for i := range e.Values {
		if e.Values[i].Key == key {
			return i
		}
	}
	return -1
}
477693
func (db *RedisDB) ssetMembers(key string) []string {
	ss, ok := db.sortedsetKeys[key]
	if !ok {
		return nil
	}
	elems := ss.byScore(asc)
	members := make([]string, 0, len(elems))
	for _, e := range elems {
		members = append(members, e.member)
	}
	return members
}
206835
func sortedKeys(m interface{}) []string {
	v := reflect.ValueOf(m)
	keys := make([]string, v.Len())

	mapKeys := v.MapKeys()
	for i, k := range mapKeys {
		keys[i] = k.Interface().(string)
	}

	sort.Strings(keys)
	return keys
}