/** * 根据给定的条件,把 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); }
/** * sort the result set for the user locale * * @param locale * The locale to use. * @param results * the result * @param <L> * the list item */ @SuppressWarnings("unchecked") public static <L extends TagData> void sortResults(Locale locale, List<L> results) { if (results.size() == 0) { return; } Collator primaryCollator = Collator.getInstance(locale); primaryCollator.setStrength(Collator.SECONDARY); Collator secondaryCollator = Collator.getInstance(locale); secondaryCollator.setStrength(Collator.TERTIARY); ComparatorChain chain = new ComparatorChain(); chain.addComparator(new BeanComparator("name", primaryCollator)); chain.addComparator(new BeanComparator("name", secondaryCollator)); Collections.sort(results, chain); }
@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; }
@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; }
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; }
@SuppressWarnings("unchecked") public static void sortByProperties(List<? extends Object> list, boolean isNullHigh, boolean isReversed, String... props) { if (CollectionUtils.isNotEmpty(list)) { Comparator<?> typeComp = ComparableComparator.getInstance(); if (isNullHigh) { 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); } } }
/** * {@inheritDoc} */ @Override protected Comparator<NormalizedRankListItem<RankTagListItem>> getComparator(Locale locale) { Collator primaryCollator = Collator.getInstance(locale); primaryCollator.setStrength(Collator.SECONDARY); Collator secondaryCollator = Collator.getInstance(locale); secondaryCollator.setStrength(Collator.TERTIARY); ComparatorChain chain = new ComparatorChain(); chain.addComparator(new BeanComparator("item.name", primaryCollator)); chain.addComparator(new BeanComparator("item.name", secondaryCollator)); return chain; }
/** * {@inheritDoc} */ @Override protected Comparator<NormalizedRankListItem<RankUserListItem>> getComparator(Locale locale) { ComparatorChain chain = new ComparatorChain(); Collator collator = Collator.getInstance(locale); collator.setStrength(Collator.SECONDARY); chain.addComparator(new BeanComparator("item.lastName", collator)); chain.addComparator(new BeanComparator("item.firstName", collator)); chain.addComparator(new BeanComparator("item.email", collator)); chain.addComparator(new BeanComparator("item.alias", collator)); return chain; }
/** * The comparator to use for sorting the resulting items. If null they will not be sorted. * * @return The comparator */ private static Comparator<Object> getComparator() { Collator primaryCollator = Collator.getInstance(Locale.ENGLISH); primaryCollator.setStrength(Collator.IDENTICAL); ComparatorChain chain = new ComparatorChain(); chain.addComparator(new BeanComparator("rawOffset")); chain.addComparator(new BeanComparator("sortingName", primaryCollator)); return chain; }
/** * The comparator to use for sorting the resulting items. If null they will not be sorted. * * @param locale * the locale * @return The comparator */ @SuppressWarnings("rawtypes") private Comparator getComparator(Locale locale) { Collator primaryCollator = Collator.getInstance(locale); primaryCollator.setStrength(Collator.SECONDARY); Collator secondaryCollator = Collator.getInstance(locale); secondaryCollator.setStrength(Collator.TERTIARY); ComparatorChain chain = new ComparatorChain(); chain.addComparator(new BeanComparator(CountryConstants.NAME, primaryCollator)); chain.addComparator(new BeanComparator(CountryConstants.NAME, secondaryCollator)); return chain; }
/** * 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; }
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; }
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); }
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; }
@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); }
@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); }
@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); }
@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); }
@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); }
@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); }
@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); }
@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); }
@Override public int compare(AcademicServiceRequest o1, AcademicServiceRequest o2) { final ComparatorChain comparatorChain = new ComparatorChain(); comparatorChain.addComparator(EXECUTION_YEAR_COMPARATOR); comparatorChain.addComparator(DomainObjectUtil.COMPARATOR_BY_ID); return comparatorChain.compare(o1, o2); }
@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); }
@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); }
@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); }
@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); }
@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); }
@EntryPoint public ActionForward prepareInsertExecutionCourse(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { List infoExecutionPeriods = null; infoExecutionPeriods = ReadExecutionPeriods.run(); if (infoExecutionPeriods != null && !infoExecutionPeriods.isEmpty()) { // exclude closed execution periods infoExecutionPeriods = (List) CollectionUtils.select(infoExecutionPeriods, new Predicate() { @Override public boolean evaluate(Object input) { InfoExecutionPeriod infoExecutionPeriod = (InfoExecutionPeriod) input; if (!infoExecutionPeriod.getState().equals(PeriodState.CLOSED)) { return true; } return false; } }); ComparatorChain comparator = new ComparatorChain(); comparator.addComparator(new BeanComparator("infoExecutionYear.year"), true); comparator.addComparator(new BeanComparator("name"), true); Collections.sort(infoExecutionPeriods, comparator); List<LabelValueBean> executionPeriodLabels = new ArrayList<LabelValueBean>(); CollectionUtils.collect(infoExecutionPeriods, new Transformer() { @Override public Object transform(Object arg0) { InfoExecutionPeriod infoExecutionPeriod = (InfoExecutionPeriod) arg0; LabelValueBean executionPeriod = new LabelValueBean(infoExecutionPeriod.getName() + " - " + infoExecutionPeriod.getInfoExecutionYear().getYear(), infoExecutionPeriod.getExternalId() .toString()); return executionPeriod; } }, executionPeriodLabels); request.setAttribute(PresentationConstants.LIST_EXECUTION_PERIODS, executionPeriodLabels); List<LabelValueBean> entryPhases = new ArrayList<LabelValueBean>(); for (EntryPhase entryPhase : EntryPhase.values()) { LabelValueBean labelValueBean = new LabelValueBean(entryPhase.getLocalizedName(), entryPhase.getName()); entryPhases.add(labelValueBean); } request.setAttribute("entryPhases", entryPhases); } return mapping.findForward("insertExecutionCourse"); }
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); }