it all depends on your data stucture.
for example, here is one way to do it
and here is another, equally as difficult/easy which i like better