org.apache.commons.collections.comparators.ComparatorChain Java Examples

The following examples show how to use org.apache.commons.collections.comparators.ComparatorChain. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExcelUtil.java    From jframework with Apache License 2.0 6 votes vote down vote up
private static void sortByProperties(List<? extends Object> list, boolean isNullHigh,
                                     boolean isReversed, String... props) {
    if (CollectionUtils.isNotEmpty(list)) {
        Comparator<?> typeComp = ComparableComparator.getInstance();
        if (isNullHigh == true) {
            typeComp = ComparatorUtils.nullHighComparator(typeComp);
        } else {
            typeComp = ComparatorUtils.nullLowComparator(typeComp);
        }
        if (isReversed) {
            typeComp = ComparatorUtils.reversedComparator(typeComp);
        }

        List<Object> sortCols = new ArrayList<Object>();

        if (props != null) {
            for (String prop : props) {
                sortCols.add(new BeanComparator(prop, typeComp));
            }
        }
        if (sortCols.size() > 0) {
            Comparator<Object> sortChain = new ComparatorChain(sortCols);
            Collections.sort(list, sortChain);
        }
    }
}
 
Example #2
Source File: MyBeanUtils.java    From spring-boot with Apache License 2.0 6 votes vote down vote up
/**
 * 根据给定的条件,把 list 中的 javabean 排序。
 * 用到了 commons beanutils 和  commons.collections
 *
 * @param list           待排序的 list
 * @param listOrderedMap 排序条件。
 *                       这是一个有序的 list ,排序条件按照加入到 list 的 bean 的属性(map 的 key)的先后顺序排序。
 *                       listOrderedMap 的 key 为待排序的 bean 的属性名称,值为是否按该属性的正序排序,true 为正序,false 为逆序。
 *                       使用方法见本类的 testSortListBeans() 方法例子,使用时注意不要写错 bean 的属性名称。
 * @param <T>            list 中的 bean 类型
 */
public static <T> void sortListBeans(List<T> list, ListOrderedMap listOrderedMap) {

    int num = listOrderedMap.size();
    ArrayList sortFields = new ArrayList();

    for (int i = 0; i < num; i++) {
        //  System.out.println("key =" + listOrderedMap.get(i) + " , value=" + listOrderedMap.getValue(i));
        Comparator comp = ComparableComparator.getInstance();

        comp = ComparatorUtils.nullLowComparator(comp);  //允许null

        if ((Boolean) listOrderedMap.getValue(i) == false)
            comp = ComparatorUtils.reversedComparator(comp); //逆序

        Comparator cmp = new BeanComparator((String) listOrderedMap.get(i), comp);
        sortFields.add(cmp);
    }

    ComparatorChain multiSort = new ComparatorChain(sortFields);
    Collections.sort(list, multiSort);
}
 
Example #3
Source File: ReadActiveCurricularCourseScopeByDegreeCurricularPlanAndExecutionYear.java    From fenixedu-academic with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Atomic
public static SortedSet<DegreeModuleScope> run(String degreeCurricularPlanID, AcademicInterval academicInterval)
        throws FenixServiceException {
    final DegreeCurricularPlan degreeCurricularPlan = FenixFramework.getDomainObject(degreeCurricularPlanID);

    final ComparatorChain comparator = new ComparatorChain();
    comparator.addComparator(new BeanComparator("curricularYear"));
    comparator.addComparator(new BeanComparator("curricularSemester"));
    comparator.addComparator(new BeanComparator("curricularCourse.externalId"));
    comparator.addComparator(new BeanComparator("branch"));

    final SortedSet<DegreeModuleScope> scopes = new TreeSet<DegreeModuleScope>(comparator);

    for (DegreeModuleScope degreeModuleScope : degreeCurricularPlan.getDegreeModuleScopes()) {
        if (degreeModuleScope.isActiveForAcademicInterval(academicInterval)) {
            scopes.add(degreeModuleScope);
        }
    }

    return scopes;
}
 
Example #4
Source File: ReadActiveCurricularCourseScopeByDegreeCurricularPlanAndExecutionYear.java    From fenixedu-academic with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Atomic
@Deprecated
public static SortedSet<DegreeModuleScope> run(String degreeCurricularPlanID, String executioYearID)
        throws FenixServiceException {
    final DegreeCurricularPlan degreeCurricularPlan = FenixFramework.getDomainObject(degreeCurricularPlanID);
    final ExecutionYear executionYear = FenixFramework.getDomainObject(executioYearID);

    final ComparatorChain comparator = new ComparatorChain();
    comparator.addComparator(new BeanComparator("curricularYear"));
    comparator.addComparator(new BeanComparator("curricularSemester"));
    comparator.addComparator(new BeanComparator("curricularCourse.externalId"));
    comparator.addComparator(new BeanComparator("branch"));

    final SortedSet<DegreeModuleScope> scopes = new TreeSet<DegreeModuleScope>(comparator);

    for (DegreeModuleScope degreeModuleScope : degreeCurricularPlan.getDegreeModuleScopes()) {
        if (degreeModuleScope.isActiveForExecutionYear(executionYear)) {
            scopes.add(degreeModuleScope);
        }
    }

    return scopes;
}
 
Example #5
Source File: DisplayEvaluationsForStudentToEnrol.java    From fenixedu-academic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public List<SelectItem> getExecutionPeriodsLabels() {
    if (this.executionPeriodsLabels == null) {
        this.executionPeriodsLabels = new ArrayList();

        final List<InfoExecutionPeriod> infoExecutionPeriods = getExecutionPeriods();
        final ComparatorChain comparatorChain = new ComparatorChain();
        comparatorChain.addComparator(new ReverseComparator(new BeanComparator("infoExecutionYear.year")));
        comparatorChain.addComparator(new ReverseComparator(new BeanComparator("semester")));
        Collections.sort(infoExecutionPeriods, comparatorChain);
        for (final InfoExecutionPeriod infoExecutionPeriod : infoExecutionPeriods) {
            final SelectItem selectItem = new SelectItem();
            selectItem.setValue(infoExecutionPeriod.getExternalId());
            selectItem.setLabel(infoExecutionPeriod.getName() + " - " + infoExecutionPeriod.getInfoExecutionYear().getYear());
            this.executionPeriodsLabels.add(selectItem);
        }
    }
    return this.executionPeriodsLabels;
}
 
Example #6
Source File: CoordinatorEvaluationManagementBackingBean.java    From fenixedu-academic with GNU Lesser General Public License v3.0 6 votes vote down vote up
public List<SelectItem> getExecutionPeriodsLabels() {
    if (this.executionPeriodsLabels == null) {
        this.executionPeriodsLabels = new ArrayList();

        final List<InfoExecutionPeriod> infoExecutionPeriods = getExecutionPeriods();
        final ComparatorChain comparatorChain = new ComparatorChain();
        comparatorChain.addComparator(new ReverseComparator(new BeanComparator("infoExecutionYear.year")));
        comparatorChain.addComparator(new ReverseComparator(new BeanComparator("semester")));
        Collections.sort(infoExecutionPeriods, comparatorChain);
        for (final InfoExecutionPeriod infoExecutionPeriod : infoExecutionPeriods) {
            final SelectItem selectItem = new SelectItem();
            selectItem.setValue(infoExecutionPeriod.getExternalId());
            selectItem.setLabel(infoExecutionPeriod.getName() + " - " + infoExecutionPeriod.getInfoExecutionYear().getYear());
            this.executionPeriodsLabels.add(selectItem);
        }
    }
    return this.executionPeriodsLabels;
}
 
Example #7
Source File: ExecutionDegree.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public int compare(ExecutionDegree o1, ExecutionDegree o2) {
    final ComparatorChain comparatorChain = new ComparatorChain();
    comparatorChain.addComparator(EXECUTION_DEGREE_COMPARATORY_BY_DEGREE_TYPE_AND_NAME);
    comparatorChain.addComparator(EXECUTION_DEGREE_COMPARATORY_BY_YEAR);

    return comparatorChain.compare(o1, o2);
}
 
Example #8
Source File: FunUtil.java    From youkefu with Apache License 2.0 5 votes vote down vote up
/**
 * Sorts a list of members according to a list of SortKeySpecs.
 * An in-place, Stable sort.
 * Helper function for MDX OrderSet function.
 *
 * <p>NOTE: Does not preserve the contents of the validator.
 */
static List<Member> sortMembers(
    Evaluator evaluator,
    Iterable<Member> memberIter,
    List<Member> memberList,
    List<SortKeySpec> keySpecList)
{
    if ((memberList != null) && (memberList.size() <= 1)) {
        return memberList;
    }
    if (memberList == null) {
        memberList = new ArrayList<Member>();
        for (Member member : memberIter) {
            memberList.add(member);
        }
        if (memberList.size() <= 1) {
            return memberList;
        }
    }

    ComparatorChain chain = new ComparatorChain();
    for (SortKeySpec key : keySpecList) {
        boolean brk = key.direction.brk;
        MemberComparator comp;
        if (brk) {
            comp = new BreakMemberComparator(
                evaluator, key.key, key.direction.descending);
        } else {
            comp = new HierarchicalMemberComparator(
                evaluator, key.key, key.direction.descending);
        }
        comp.preloadValues(memberList);
        chain.addComparator(comp.wrap(), false);
    }

    Collections.sort(memberList, chain);
    return memberList;
}
 
Example #9
Source File: Party.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public int compare(final Party o1, final Party o2) {
    final ComparatorChain comparatorChain = new ComparatorChain();
    comparatorChain.addComparator(Party.COMPARATOR_BY_SUBPARTY);
    comparatorChain.addComparator(Party.COMPARATOR_BY_NAME);
    comparatorChain.addComparator(DomainObjectUtil.COMPARATOR_BY_ID);
    return comparatorChain.compare(o1, o2);
}
 
Example #10
Source File: Party.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public int compare(final Party o1, final Party o2) {
    final ComparatorChain comparatorChain = new ComparatorChain();
    comparatorChain.addComparator(Party.COMPARATOR_BY_NAME);
    comparatorChain.addComparator(DomainObjectUtil.COMPARATOR_BY_ID);

    return comparatorChain.compare(o1, o2);
}
 
Example #11
Source File: MarkSheet.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
final public int compare(MarkSheet o1, MarkSheet o2) {
    final ComparatorChain comparatorChain = new ComparatorChain();
    comparatorChain.addComparator(MarkSheet.COMPARATOR_BY_EVALUATION_DATE);
    comparatorChain.addComparator(MarkSheet.COMPARATOR_BY_CREATION_DATE);
    comparatorChain.addComparator(DomainObjectUtil.COMPARATOR_BY_ID);

    return comparatorChain.compare(o1, o2);
}
 
Example #12
Source File: MarkSheet.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
final public int compare(MarkSheet o1, MarkSheet o2) {
    final ComparatorChain comparatorChain = new ComparatorChain();
    comparatorChain.addComparator(MarkSheet.COMPARATOR_BY_EVALUATION_DATE);
    comparatorChain.addComparator(DomainObjectUtil.COMPARATOR_BY_ID);

    return comparatorChain.compare(o1, o2);
}
 
Example #13
Source File: ExternalEnrolment.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public int compare(ExternalEnrolment o1, ExternalEnrolment o2) {
    final ComparatorChain comparatorChain = new ComparatorChain();
    comparatorChain.addComparator(ExternalEnrolment.COMPARATOR_BY_EXECUTION_PERIOD_AND_ID);
    comparatorChain.addComparator(ExternalEnrolment.COMPARATOR_BY_APPROVEMENT_DATE);

    return comparatorChain.compare(o1, o2);
}
 
Example #14
Source File: CycleCurriculumGroup.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
final public int compare(final CycleCurriculumGroup o1, final CycleCurriculumGroup o2) {
    final ComparatorChain comparatorChain = new ComparatorChain();
    comparatorChain.addComparator(CycleCurriculumGroup.COMPARATOR_BY_CYCLE_TYPE);
    comparatorChain.addComparator(DomainObjectUtil.COMPARATOR_BY_ID);

    return comparatorChain.compare(o1, o2);
}
 
Example #15
Source File: ConclusionProcessVersion.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
final public int compare(ConclusionProcessVersion o1, ConclusionProcessVersion o2) {
    final ComparatorChain chain = new ComparatorChain();
    chain.addComparator(ConclusionProcessVersion.COMPARATOR_BY_CREATION_DATE_TIME);
    chain.addComparator(DomainObjectUtil.COMPARATOR_BY_ID);
    return chain.compare(o1, o2);
}
 
Example #16
Source File: ICurriculumEntry.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
final public int compare(ICurriculumEntry o1, ICurriculumEntry o2) {
    final ComparatorChain comparatorChain = new ComparatorChain();
    comparatorChain.addComparator(ICurriculumEntry.COMPARATOR_BY_EXECUTION_YEAR_AND_NAME);
    comparatorChain.addComparator(ICurriculumEntry.COMPARATOR_BY_ID);

    return comparatorChain.compare(o1, o2);
}
 
Example #17
Source File: ICurriculumEntry.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
final public int compare(ICurriculumEntry o1, ICurriculumEntry o2) {
    final ComparatorChain comparatorChain = new ComparatorChain();
    comparatorChain.addComparator(ICurriculumEntry.COMPARATOR_BY_REVERSE_EXECUTION_PERIOD_AND_NAME);
    comparatorChain.addComparator(ICurriculumEntry.COMPARATOR_BY_ID);

    return comparatorChain.compare(o1, o2);
}
 
Example #18
Source File: ICurriculumEntry.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
final public int compare(ICurriculumEntry o1, ICurriculumEntry o2) {
    final ComparatorChain comparatorChain = new ComparatorChain();
    comparatorChain.addComparator(ICurriculumEntry.COMPARATOR_BY_EXECUTION_PERIOD_AND_NAME);
    comparatorChain.addComparator(ICurriculumEntry.COMPARATOR_BY_ID);

    return comparatorChain.compare(o1, o2);
}
 
Example #19
Source File: ICurriculumEntry.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
final public int compare(ICurriculumEntry o1, ICurriculumEntry o2) {
    final ComparatorChain comparatorChain = new ComparatorChain();
    comparatorChain.addComparator(ICurriculumEntry.COMPARATOR_BY_EXECUTION_PERIOD);
    comparatorChain.addComparator(ICurriculumEntry.COMPARATOR_BY_ID);

    return comparatorChain.compare(o1, o2);
}
 
Example #20
Source File: IndividualCandidacyPersonalDetails.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public int compare(final IndividualCandidacyPersonalDetails o1, final IndividualCandidacyPersonalDetails o2) {
    final ComparatorChain comparatorChain = new ComparatorChain();
    comparatorChain.addComparator(COMPARATOR_BY_NAME);
    comparatorChain.addComparator(DomainObjectUtil.COMPARATOR_BY_ID);

    return comparatorChain.compare(o1, o2);
}
 
Example #21
Source File: AcademicServiceRequestsManagementDispatchAction.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Comparator getComparator(HttpServletRequest request) {
    final String orderParameter = request.getParameter(ORDER_PARAMETER);
    final String orderGetter =
            StringUtils.isEmpty(orderParameter) ? DEFAULT_ORDER_GETTER : orderParameter.substring(0,
                    orderParameter.indexOf(ORDER_MARKER));

    final String orderDir =
            StringUtils.isEmpty(orderParameter) ? DEFAULT_ORDER_DIR : orderParameter.substring(
                    orderParameter.indexOf(ORDER_MARKER) + 1, orderParameter.length());
    final boolean orderAsc = Arrays.asList(ASC_ORDER_DIR).contains(orderDir);

    if (orderGetter.equals(REQUEST_NUMBER_YEAR)) {
        return orderAsc ? AcademicServiceRequest.COMPARATOR_BY_NUMBER : new ReverseComparator(
                AcademicServiceRequest.COMPARATOR_BY_NUMBER);
    } else if (orderGetter.equals(EXECUTION_YEAR)) {
        return orderAsc ? AcademicServiceRequest.EXECUTION_YEAR_AND_OID_COMPARATOR : new ReverseComparator(
                AcademicServiceRequest.EXECUTION_YEAR_AND_OID_COMPARATOR);
    } else if (orderGetter.equals(REGISTRATION_NUMBER) || orderGetter.equals(DESCRIPTION)
            || orderGetter.equals(URGENT_REQUEST) || orderGetter.equals(REGISTRATION_NUMBER)
            || orderGetter.equals(REQUEST_DATE) || orderGetter.equals(ACTIVE_SITUATION_DATE)) {
        final ComparatorChain chain = new ComparatorChain();
        chain.addComparator(orderAsc ? new BeanComparator(orderGetter) : new ReverseComparator(
                new BeanComparator(orderGetter)));
        chain.addComparator(DomainObjectUtil.COMPARATOR_BY_ID);
        return chain;
    }

    return null;
}
 
Example #22
Source File: DisplayCurricularPlan.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void sortScopes(List<InfoCurricularCourseScope> scopes) {
    ComparatorChain comparatorChain = new ComparatorChain();
    comparatorChain.addComparator(new BeanComparator("infoCurricularSemester.infoCurricularYear.year"));
    comparatorChain.addComparator(new BeanComparator("infoBranch.name"));
    comparatorChain.addComparator(new BeanComparator("infoCurricularSemester.semester"));
    comparatorChain.addComparator(new BeanComparator("infoCurricularCourse.name"));
    Collections.sort(scopes, comparatorChain);
}
 
Example #23
Source File: InfoStudentCurricularPlan.java    From fenixedu-academic with GNU Lesser General Public License v3.0 5 votes vote down vote up
public List getInfoEnrolmentsSorted() {
    final List<InfoEnrolment> infoEnrolments = getInfoEnrolments();
    ComparatorChain comparatorChain = new ComparatorChain();
    comparatorChain.addComparator(new BeanComparator("infoExecutionPeriod.infoExecutionYear.year"));
    comparatorChain.addComparator(new BeanComparator("infoExecutionPeriod.semester"));
    comparatorChain.addComparator(new BeanComparator("infoCurricularCourse.name", Collator.getInstance()));
    Collections.sort(infoEnrolments, comparatorChain);
    return infoEnrolments;
}
 
Example #24
Source File: ComparatorUtils.java    From Penetration_Testing_POC with Apache License 2.0 5 votes vote down vote up
/**
 * Gets a comparator that compares using an array of {@link Comparator}s, applied
 * in sequence until one returns not equal or the array is exhausted.
 *
 * @param comparators  the comparators to use, not null or empty or containing nulls
 * @return a {@link ComparatorChain} formed from the input comparators
 * @throws NullPointerException if comparators array is null or contains a null
 * @see ComparatorChain
 */
public static Comparator chainedComparator(Comparator[] comparators) {
    ComparatorChain chain = new ComparatorChain();
    for (int i = 0; i < comparators.length; i++) {
        if (comparators[i] == null) {
            throw new NullPointerException("Comparator cannot be null");
        }
        chain.addComparator(comparators[i]);
    }
    return chain;
}
 
Example #25
Source File: CurricularCourseEquivalenciesDA.java    From fenixedu-academic with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void sortInfoCurricularCourseEquivalences(final List<CurricularCourseEquivalence> equivalences) {
    final ComparatorChain chain = new ComparatorChain();
    chain.addComparator(CurricularCourseEquivalence.COMPARATOR_BY_EQUIVALENT_COURSE_NAME);
    chain.addComparator(CurricularCourseEquivalence.COMPARATOR_BY_EQUIVALENT_COURSE_CODE);
    Collections.sort(equivalences, chain);
}