Find closest NPC

How do I find closest NPC from given position?

[lua]
local npcs = {}

for k,v in pairs(ents.FindByClass(“npc_*”)) do
if( v:IsValid() && v:IsNPC() && v:Health()>0) then
table.insert(npcs,v)
end
end

table.sort(npcs, function(a, b) return self:GetPos():Distance(b:GetPos()) < self:GetPos():Distance(b:GetPos()) end)
[/lua]
Does not work.

you made a typo

[lua]self:GetPos():Distance(b:GetPos()) < self:GetPos():Distance(b:GetPos())[/lua]

should be

[lua]self:GetPos():Distance(a:GetPos()) < self:GetPos():Distance(b:GetPos())[/lua]