@@ -0,0 +1,2101 @@
+package org.commcare.cases.entity;%0A%0Aimport org.commcare.suite.model.Detail;%0Aimport org.javarosa.core.model.instance.TreeReference;%0A%0Aimport java.util.List;%0A%0A/**%0A * Created by amstone326 on 5/24/17.%0A */%0Apublic abstract class SortableEntityAdapter implements EntitySortNotificationInterface %7B%0A%0A    private List%3CEntity%3CTreeReference%3E%3E entities;%0A    private Detail detail;%0A    private int%5B%5D currentSort = %7B%7D;%0A    private boolean reverseSort = false;%0A    protected boolean asyncMode = false;%0A%0A    SortableEntityAdapter(List%3CEntity%3CTreeReference%3E%3E entityList, Detail detail,%0A                          boolean asyncMode) %7B%0A        this.entities = entityList;%0A        this.detail = detail;%0A        this.asyncMode = asyncMode;%0A%0A        int%5B%5D orderedFieldsForSorting = determineFieldsForSortingInOrder();%0A        if (!this.asyncMode && orderedFieldsForSorting.length != 0) %7B%0A            sort(orderedFieldsForSorting);%0A        %7D%0A    %7D%0A%0A    private int%5B%5D determineFieldsForSortingInOrder() %7B%0A        int%5B%5D fieldsForSorting = detail.getOrderedFieldIndicesForSorting();%0A        if (fieldsForSorting.length == 0) %7B%0A            for (int i = 0; i %3C detail.getFields().length; ++i) %7B%0A                String header = detail.getFields()%5Bi%5D.getHeader().evaluate();%0A                if (!%22%22.equals(header)) %7B%0A                    fieldsForSorting = new int%5B%5D%7Bi%7D;%0A                    break;%0A                %7D%0A            %7D%0A        %7D%0A        return fieldsForSorting;%0A    %7D%0A%0A    protected void sort(int%5B%5D fields) %7B%0A        //The reversing here is only relevant if there's only one sort field and we're on it%0A        sort(fields, (currentSort.length == 1 && currentSort%5B0%5D == fields%5B0%5D) && !reverseSort);%0A    %7D%0A%0A    private void sort(int%5B%5D fields, boolean reverse) %7B%0A        this.reverseSort = reverse;%0A        currentSort = fields;%0A%0A        java.util.Collections.sort(this.entities,%0A                new EntitySorter(detail.getFields(), reverseSort, currentSort, this));%0A    %7D%0A%0A    public int%5B%5D getCurrentSort() %7B%0A        return currentSort;%0A    %7D%0A%0A    public boolean isCurrentSortReversed() %7B%0A        return reverseSort;%0A    %7D%0A%7D%0A

Move SortableEntityAdapter into core