We have user reviews of products that have following things:
- Review description
- Date on which review was written
- City from where review was written
We have two tables here:
1) Reviews (Columns: Description (varchar), date (DateTime), CityId (int))
2) Cities (Columns : Id (int), Name (varchar), Lat, Long)
We want to sort these user reviews such that if input is any given city, let's say city 'C', the output sort order should be:
- Reviews written for cities within 0 to 'X' kilometres of city 'C', followed by
- Reviews written for cities in 'X' to '2X' kilometres of city 'C', followed by
- Reviews written for cities in '2X' to '3X' kilometres and so on till the cities end.
One simple approach to solve this is:Just do it on runtime and fetch cities in these kilometre ranges one by one and then review for those but it would be too slow. Also, we will do same operation multiple times. To avoid recomputation we can save the information of nearby bucket city for each city but not sure if that's ideal.
Is there any simpler and more efficient way to solve this i.e. by kat/long or any other approach?