Sort buckets by count in terms aggregation using java api client

I want to sort buckets by doc count in terms aggregation and also sort buckets by sub aggregation