// Add the reordered objects
int listItemIDCount = listItemIDArray.count();
for (int listItemIDIndex = 0; listItemIDIndex < listItemIDCount; listItemIDIndex++) {
String itemID = (String) listItemIDArray.objectAtIndex(listItemIDIndex);
NSRange itemPageRange;
if (mutableList) {
itemPageRange = new NSRange(startIndex + listItemIDIndex, listItemIDCount - listItemIDIndex);
}
else {
itemPageRange = new NSRange(startIndex, listItemIDCount);
}
NSArray itemPageArray = list.subarrayWithRange(itemPageRange);
EOQualifier itemIDQualifier = new EOKeyValueQualifier(listItemIDKeyPath, EOQualifier.QualifierOperatorEqual, itemID);
NSArray matchingItems = EOQualifier.filteredArrayWithQualifier(itemPageArray, itemIDQualifier);
if (matchingItems.count() == 0) {
throw new NoSuchElementException("There was no item that matched the ID '" + itemID + "' in " + list + ".");
}
else if (matchingItems.count() > 1) {
throw new IllegalStateException("There was more than one item that matched the ID '" + itemID + "' in " + list + ".");
}
Object replacingItem = matchingItems.objectAtIndex(0);
if (mutableList) {
int replacedItemIndex = itemPageRange.location();
Object replacedItem = reorderedList.objectAtIndex(replacedItemIndex);
if (replacedItem != replacingItem) {
int replacingItemIndex = replacedItemIndex + itemPageArray.indexOfObject(replacingItem);
reorderedList.replaceObjectAtIndex(replacingItem, replacedItemIndex);
reorderedList.replaceObjectAtIndex(replacedItem, replacingItemIndex);