This question is a follow up of this question you asked earlier.
Here we handled getting count per posttype.
So you have all data, just not sorted correctly.
Looking at your code you have one big problem.
Getting and formatting data is done at the same time as you are outputting the data.
For maintainability (and readability) it is best to separate these as ...