Order posts by years

i found this code snippet a couple of days ago. canĀ“t remember where…
copy this snippet in your archive template

<?php
                $year_prev = null;
                $months = $wpdb->get_results(   "SELECT DISTINCT MONTH( post_date ) AS month ,
                YEAR( post_date ) AS year,
                COUNT( id ) as post_count FROM $wpdb->posts
                WHERE post_status="publish" and post_date <= now( )
                and post_type="post"
                GROUP BY month , year
                ORDER BY post_date DESC");
                foreach($months as $month) :
                    $year_current = $month->year;
                    if ($year_current != $year_prev){
                        if ($year_prev != null){?>
                </ul>
                <?php } ?>
                <h3 class="archive"><?php echo $month->year; ?></h3>
                <ul class="archive-list">
                <?php } ?>
                <li>
                <a href="https://wordpress.stackexchange.com/questions/27097/<?php bloginfo("url') ?>/<?php echo $month->year; ?>/<?php echo date("m", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>">
                <span class="archive-month"><?php echo date("F", mktime(0, 0, 0, $month->month, 1, $month->year)) ?></span>
                <span class="archive-count"><?php echo $month->post_count; ?></span>
                </a>
                </li>
                <?php $year_prev = $year_current;
                endforeach; ?>
                </ul>