Q:

Get Previous item in a list

I've a previous/next setup in the works, and the next functionality is working fine (although, that was created by someone else a while back). As for the previous button, that is giving me a few problems.
Here is the code so far:
    private Item getPrevious()
    {
        Item CurrentItem = Sitecore.Context.Item;
        var blogHomeID = "{751B0E3D-26C2-489A-8B8C-8D40E086A970}";
        Item BlogItem =db.Items.GetItem(blogHomeID);
        Item[] EntryList= BlogItem.Axes.SelectItems("descendant::*[@@templatename='BlogEntry']");

        Item prevEntry = null;

        for (int i = 0; i < EntryList.Length; i++)
        {
            if (EntryList[i] == CurrentItem)
            {
                return prevEntry;
            }

            prevEntry = EntryList[i];
        }
    }

I get that you need to subtract 1 from the current item in the list to get the previous, but so far, all this seems to do is display the exact same entry for the previous button. It's always the latest entry in the list, not the previous one. I feel like this shouldn't be so difficult but it could be the old code I'm trying to work with. Not sure.

A:

You can use the GetPreviousSibling() and GetNextSibling() methods:
Sitecore.Context.Item.Axes.GetPreviousSibling()

Sitecore.Context.Item.Axes.GetNextSibling()

These will return the previous and next sibling, or null if the current item is the first/last respectively.
If you want to restrict by template type then you can use the preceding and following xpath queries:
Item previous = Sitecore.Context.Item.Axes.SelectItems("./preceding::*[@@templateid='{template-guid}']").LastOrDefault();

Item next = Sitecore.Context.Item.Axes.SelectSingleItem("./following::*[@@templateid='{template-guid}']");

Note the use of SelectItems and LastOrDefault() on the first query. Both queries give you a list of Items sorted by order.

