am e152520f: am ef80bd54: Merge "Fix dependency sorting in CoordinatorLayout" into mnc-ub-dev

* commit 'e152520f897e91ec95e3ca558fbd78c5139879b8':
  Fix dependency sorting in CoordinatorLayout

GitOrigin-RevId=e556b1b18ca90f61b653277fe3e043f27a9830cd

PiperOrigin-RevId: 140550159
This commit is contained in:
Chris Banes 2016-11-30 01:32:49 +00:00 committed by Travis Collins
parent e3918bee1a
commit 0afb77643d

View File

@ -566,7 +566,9 @@ public class CoordinatorLayout extends ViewGroup implements NestedScrollingParen
for (int i = 0; i < childCount; i++) {
mDependencySortedChildren.add(getChildAt(i));
}
Collections.sort(mDependencySortedChildren, mLayoutDependencyComparator);
// We need to use a selection sort here to make sure that every item is compared
// against each other
selectionSort(mDependencySortedChildren, mLayoutDependencyComparator);
}
}
@ -2637,4 +2639,37 @@ public class CoordinatorLayout extends ViewGroup implements NestedScrollingParen
}
};
}
private static void selectionSort(final List<View> list, final Comparator<View> comparator) {
if (list == null || list.size() < 2) {
return;
}
final View[] array = new View[list.size()];
list.toArray(array);
final int count = array.length;
for (int i = 0; i < count; i++) {
int min = i;
for (int j = i + 1; j < count; j++) {
if (comparator.compare(array[j], array[min]) < 0) {
min = j;
}
}
if (i != min) {
// We have a different min so swap the items
final View minItem = array[min];
array[min] = array[i];
array[i] = minItem;
}
}
// Finally add the array back into the collection
list.clear();
for (int i = 0; i < count; i++) {
list.add(array[i]);
}
}
}