From 0afb77643d3c331c7a36328ea2950812f8025905 Mon Sep 17 00:00:00 2001 From: Chris Banes Date: Wed, 30 Nov 2016 01:32:49 +0000 Subject: [PATCH] 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 --- .../design/widget/CoordinatorLayout.java | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/android/support/design/widget/CoordinatorLayout.java b/src/android/support/design/widget/CoordinatorLayout.java index a4e1f8a92..359c468b0 100644 --- a/src/android/support/design/widget/CoordinatorLayout.java +++ b/src/android/support/design/widget/CoordinatorLayout.java @@ -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 list, final Comparator 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]); + } + } }