<script>
let arr=[
{id:1,name:"aaa",age:12},
{id:2,name:"bbb",age:20},
{id:3,name:"ccc",age:5},
{id:4,name:"ddd",age:18}
]
function sortKey(array,key){
return array.sort(function(a,b){
return a[key]-b[key]
})
}
console.log(sortKey(arr,'age'))
</script>