This is indeed very similar to how initial sorting of the database is done. To improve accuracy (over performance) further sorting is done.
Like mentioned the non-linearity with regard to latitude makes standard trigonometry useless. In my case I opted for the haversine formula (https://en.wikipedia.org/wiki/Haversine_formula) this is pretty accurate on the small range of 1km