How add sorting by Flag counter in Controller? I get node list in my Conroller:

$query = \Drupal::entityQuery('node')
  -&gt;condition('status', 1)
  -&gt;pager(10)
  -&gt;sort('created' , 'DESC');
$nids = $query-&gt;execute();


How can add sorting by Flag counter?
The flag count in another table called flag_counts so you should do join with node table try the following:

$database = \Drupal::database();
$query = $database-&gt;select('node', 'n');
$query-&gt;fields('n', [])
  -&gt;condition('nfd.status', '1', '=');
// Inner join to get the node status.
$query-&gt;innerJoin('node_field_data', 'nfd', 'nfd.nid = n.nid');
// This join to get the flag counts by node.
$query-&gt;leftJoin('flag_counts', 'fc', 'n.nid = fc.entity_id');
$query-&gt;fields('fc', ['entity_id', 'count']);
// Sort by count.
$query-&gt;orderBy('fc.count', 'DESC');
$query-&gt;range(0, 10);
$result = $query-&gt;execute()-&gt;fetchAll();