A recent question in chat about "When you are in Aachen, Germany, five other countries capitals are closer than Berlin." triggered me to try to write something to find the cities automatically for whatever city I like.
At first, I get all UN countries and their respective capitals.
countries = CountryData["UN"];
capitals = Map[CountryData[#, "CapitalCity"] &, countries];
capitalCoord =
Select[Map[{#, CityData[#, "Coordinates"]} &, capitals],
NumericQ[#[[2, 1]]] &];
The "Select..."-part in capitalCoord is due to the fact, that some countries (before I only selected UN countries) did not have a listed capital. I just left it in, because this is only executed once.
Second is to define a reference city (here Aachen) and based on the found capitals above to get all distances to all capitals, sorting them, and show all closer and the own capital.
refCity = CityData[{"Aachen", "NorthRhineWestphalia", "Germany"}];
ref = GeoPosition[refCity];
erg = Map[{#[[1]], GeoDistance[ref, #[[2]]]} &, capitalCoord];
With[{l = Sort[erg, #1[[2]] < #2[[2]] &],
city = CountryData[CityData[refCity, "Country"], "CapitalCity"]},
For[i = 1, i <= Length[countries], i++,
If[l[[i, 1]] == city, Break[]]]];
GeoGraphics[Insert[With[{l = Sort[erg, #1[[2]] < #2[[2]] &][[;; i]]},
Table[GeoPath[{l[[j, 1]], refCity}], {j, i}]], Red, i],
GeoProjection -> "Robinson",
PlotLabel -> ToString[i - 1] <> " UN nation capitals are closer"]
Some examples (Red is always the own capital):
Aachen, Germany

There are actually seven capitals closer than Berlin. Then we were trying more fancy cities …
My question is now … again … what would be a more elegant way to do it?
If you can do it more fancy, please do. :)











With[{city = Entity["City", {"Aachen", "NorthRhineWestphalia", "Germany"}]}, LengthWhile[GeoNearest[CountryData[#, "CapitalCity"] & /@ CountryData["UN"], city, All], CityData[city, "Country"] =!= CityData[#, "Country"] &]]– J. M.'s missing motivation Nov 03 '17 at 03:00