'mongodb get collection.aggregate reponse sorted

i have a nodejs server with mongodb database and a collection with the following structure: {user_id: int, country: string} i need to get the country name that have the most users from. currently what i do is get collection.distinct to get unique countries, and after that i run aggregate query to get the count and finally i sort the response_array manually to get the highest count, my question is what i need to add to the query in order to get the data sorted from the database so i wont need to sort it after the response?

let query = {};
for (let i = 0; i < countries.length; i++)
{
    let country = countries[i];
    query[country] = [{
        $match: { country: country}
    }, { $count: "count" }]
}
let response_cursor = await collection.aggregate([{$facet: query}]);
let response_array = await response_cursor.toArray();
//sorting the response_array


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source