Sort one field wrt another

Hi
How would i sort my search based on one field in the id's, I tried setting up using sort command and output just id's but it was not working. Basically i want top 4 id's based on one particular metric

GET /ind/table/_search/
{
"sort": [
{
"val": {"order" : "asc"}
}
],
"query": {
"term": { "id" }
},
}

Do we have to use aggregation, can i get example based on it?

Thanks again

Can you be more specific about what is not working?

Thanks for the reply.
I think my current query is not the correct way to go about it. My final objective is
Suppose we have two fields in our objects: val and user. Both are not unique. I wanted to take average of val for each unique user and sort them out.
ex. user: 23, val : 1
user: 24, val: 4
user: 23, val: 8
In output:
user 23 ------ 4.5 (Average)
user 24 ------ 4 (Average)

The following can be done in kibana using metric table.