Generally speaking, you sorting algorithm should involve the following two steps:


Sort the books and assign them a numerical order.
Generate optimal sorting instructions given the permutation.


In your example, you start with a list of four books. Sorting them, you assign them a numerical order, B C A D (the numbers here are A,B,C,D). Then you figure out instructions for sorting the permutation B C A D. One approach that will always work is to move A to 0, B to 1, C to 2, and so on. However, this is not optimal. Figuring out the optimal sequence of moves seems like the difficult part of your question.

Algorithm for generating sorting instructions
I have a bit of a strange problem with my WP-query. I have a custom post type (portfolio), with a custom taxonomy called year. I have categories for each year, so what I want to do is display all posts for each year. The problem is, only 2012 works. Doesn't matter if I order the categories ASC/DESC - only 2012 works.

&lt;section id="content"&gt;
    &lt;?php
    $categories = get_categories('taxonomy=year&amp;order=DESC');
    foreach($categories as $category) : ?&gt;
    &lt;article class="year"&gt;
        &lt;h2&gt;&lt;?php echo $category-&gt;name ?&gt;&lt;/h2&gt;
        &lt;div class="items"&gt; 
        &lt;?php
        $posts = get_posts('taxonomy=year&amp;post_type=portfolio&amp;year=' . $category-&gt;slug);
        foreach($posts as $post) : ?&gt;
            &lt;div class="item"&gt;
            &lt;?php 
            $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');
            echo '&lt;a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" rel="lightbox[' . $category-&gt;slug . ']" &gt;';
            the_post_thumbnail('thumbnail');
            echo '&lt;/a&gt;';
            ?&gt;
            &lt;/div&gt;
        &lt;?php endforeach; ?&gt;                    
        &lt;/div&gt;  
    &lt;/article&gt;
    &lt;?php 
    endforeach;
    wp_reset_query();
    ?&gt;
&lt;/section&gt;


What am I doing wrong? To me, it seems right.. I've tried a bunch of different takes on this, everything from real querys to ridiculous sortings but I just can't get it right..

Thank you in advance!
