Sort posts by year and show by months on click

Currently archives on my blog shows like this:

January, 2016(1)
February, 2016(2)
February, 2015(3)
January, 2014(4)

What I want it is to be sorted by year, and only lists month of current year. It should show like this:

Read More
January, 2016(1)
February, 2016(2)
2015
2014

and so on.

MySQL query is:

   function get_dates_by_post_type( $post_type, $limit = 10 )
    {
        global $wpdb;

        $datestr = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts
            FROM $wpdb->posts 
            WHERE post_type = '$post_type'
            AND post_status = 'publish'
            GROUP BY YEAR(post_date)/*, MONTH(post_date)*/
            ORDER BY post_date DESC
            LIMIT $limit";

        $dates = $wpdb->get_results($datestr, OBJECT);

        return $dates;
    }

And code for displaying the archives:

<ul class="sub-menu-list-lvl-3">
                        <?php
                            $dates = get_dates_by_post_type($post_type);

                            foreach ($dates as $archive) {
                                $month = mktime(0, 0, 0, $archive->month, 1, 2005);
                                $the_date = date('F', $month).' '.$archive->year;

                                if(is_arabic())
                                {
                                    global $Ar;
                                    $time = strtotime($the_date);
                                    $fix = $Ar->dateCorrection($time);
                                    $Ar->setMode(2); 
                                    $the_date = find_and_replace_arabic_numbers($Ar->date('M Y', $time, $fix));
                                }

                                if ( isset( $query_vars['year'] ) && $query_vars['year'] == $archive->year && isset( $query_vars['monthnum'] ) && $query_vars['monthnum'] == $archive->month )
                                {
                                    $active = 'active';
                                }
                                else
                                {
                                    $active = '';
                                }

                                echo '<li>'.
                                    '<a class="sub-menu-lvl-3 slidingDoors '.$active.'" href="'.get_month_link( $archive->year, $archive->month ).'?pt='.$post_type.'" title="'.$the_date.'">'.
                                        '<img src="'.get_bloginfo( 'template_url' ).'/assets/img/lvl-3-sub-nav-t.png" class="btnT" alt="">'.
                                        '<span class="inlineBlock">'.$the_date.' ('.find_and_replace_arabic_numbers($archive->posts).')</span>'.
                                        '<img src="'.get_bloginfo( 'template_url' ).'/assets/img/'.$subfolder.'lvl-3-sub-nav-r.png" class="lvl-3-arrow" alt="">'.
                                        '<img src="'.get_bloginfo( 'template_url' ).'/assets/img/lvl-3-sub-nav-b.png" class="btnB" alt="">'.
                                    '</a>'.
                                '</li>';
                            }
                        ?>
                    </ul>

Related posts