As the answer in the linked question mentions, there is no guarantee that the map view's annotations
property will preserve any order.
In addition, since the annotations
property includes the userLocation
if you have showsUserLocation
turned on (but which you don't yourself explicitly call addAnnotation
for), the annotation order will not be what you may expect.
Don't rely on the order of the annotations in the map view's annotations
array.
Instead, keep your own array of references to the annotations and sort them any way you want (like your annotationSort
array).
But there's no point in removing them from the map and adding them back.
Keep in mind that the map view's annotations
array may contain the MKUserLocation
annotation so when constructing your array, check the type of each annotation before including it or accessing custom properties.
However, note that the code to sort the array:
[annotationSort sortedArrayUsingComparator:...
is flawed itself because sortedArrayUsingComparator
returns an NSArray
.
It does not sort the array in-place.
Instead, to sort an NSMutableArray
, call its sortUsingComparator
method.
Using this sorted array, your app can access or select the annotations in the order desired.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…