Skip to content

Instantly share code, notes, and snippets.

@rofr
Created April 11, 2014 10:24
Show Gist options
  • Save rofr/10456364 to your computer and use it in GitHub Desktop.
Save rofr/10456364 to your computer and use it in GitHub Desktop.
Is there a more an efficient way to get the index of an item in a SortedSet?
/// <summary>
/// Returns the rank of member in the sorted set stored at key, with the scores ordered from low to high.
/// The rank (or index) is 0-based, which means that the member with the lowest score has rank 0.
/// </summary>
/// <param name="key"></param>
/// <param name="member"></param>
/// <returns>the rank of member or null if the set or member does not exist</returns>
public int? ZRank(string key, string member)
{
SortedSet<ZSetEntry> set = GetSortedSet(key);
if (set == null) return null;
int idx = -1;
foreach (var entry in set)
{
idx++;
if (entry.Value == member) return idx;
}
return null;
}
@yreynhout
Copy link

Binary Search?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment