Example usage for java.util Collections sort

List of usage examples for java.util Collections sort

Introduction

In this page you can find the example usage for java.util Collections sort.

Prototype

@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> void sort(List<T> list, Comparator<? super T> c) 

Source Link

Document

Sorts the specified list according to the order induced by the specified comparator.

Usage

From source file:edu.mayo.cts2.framework.plugin.service.lexevs.uri.DelegatingUriHandler.java

@Override
public void afterPropertiesSet() throws Exception {
    Collections.sort(this.delegateUriHandlers, OrderComparator.INSTANCE);
}

From source file:de.tor.tribes.util.TribeUtils.java

public static Tribe[] getTribeByVillage(Village[] pVillages, boolean pUseBarbarians,
        Comparator<Tribe> pComparator) {
    List<Tribe> tribes = new LinkedList<>();

    for (Village v : pVillages) {
        Tribe t = v.getTribe();/*from ww  w. j a va  2 s. c o m*/
        if (pUseBarbarians || !t.equals(Barbarians.getSingleton())) {
            if (!tribes.contains(t)) {
                tribes.add(t);
            }
        }
    }

    if (pComparator != null) {
        Collections.sort(tribes, pComparator);
    }

    return tribes.toArray(new Tribe[tribes.size()]);
}

From source file:com.ibm.rtc.automation.examples.client.RTCUserUtil.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static void listUsers(ITeamRepository repo) throws TeamRepositoryException {
    IContributorManager icm = repo.contributorManager();
    List allContribs = icm.fetchAllContributors(null);
    //System.out.println(allContribs.size());
    ArrayList clone = new ArrayList();
    clone.addAll(allContribs);//  w ww.  j  ava 2  s  .  c  om
    Collections.sort(clone, new UserComparator());

    int allContribSize = clone.size();
    for (int i = 0; i < allContribSize; i++) {
        IContributor user = (IContributor) clone.get(i);

        System.out.println(user.getName());
    }
    System.out.println("There are " + allContribSize + " total users in the repository");
}

From source file:Main.java

/**
 * Parses the contents of {@link Engine#EXTRA_AVAILABLE_VOICES} and returns
 * a unmodifiable list of {@link Locale}s sorted by display name. See
 * {@link #LOCALE_COMPARATOR} for sorting information.
 *
 * @param availableLanguages A list of locale strings in the form
 *            {@code language-country-variant}.
 * @return A sorted, unmodifiable list of {@link Locale}s.
 *///from  w w  w . ja va  2s .c o m
public static List<Locale> parseAvailableLanguages(List<String> availableLanguages) {
    final List<Locale> results = new ArrayList<Locale>(availableLanguages.size());

    for (String availableLang : availableLanguages) {
        final String[] langCountryVar = availableLang.split("-");
        final Locale loc;

        if (langCountryVar.length == 1) {
            loc = new Locale(langCountryVar[0]);
        } else if (langCountryVar.length == 2) {
            loc = new Locale(langCountryVar[0], langCountryVar[1]);
        } else if (langCountryVar.length == 3) {
            loc = new Locale(langCountryVar[0], langCountryVar[1], langCountryVar[2]);
        } else {
            continue;
        }

        results.add(loc);
    }

    // Sort by display name, ascending case-insensitive.
    Collections.sort(results, LOCALE_COMPARATOR);

    return Collections.unmodifiableList(results);
}

From source file:org.openmrs.module.adminui.page.controller.metadata.locations.ManageLocationTagsPageController.java

/**
 * @param model/* w ww  . ja  va2  s .  c o  m*/
 * @param locationService
 */
public void get(PageModel model, @SpringBean("locationService") LocationService locationService) {
    List<LocationTag> locationTags = locationService.getAllLocationTags(true);
    Collections.sort(locationTags, new ByRetiredComparator());
    model.addAttribute("locationTags", locationTags);
}

From source file:gov.nih.nci.cabig.caaers.web.admin.AgentEditController.java

@Override
protected Object formBackingObject(HttpServletRequest request) throws Exception {
    super.formBackingObject(request);

    Agent agent = null;/*  w ww .j av a2 s.  c o m*/
    agent = agentRepository.getAgentByID(Integer.parseInt(request.getParameter("agentID")));

    AgentCommand c = new AgentCommand();
    c.setAgent(agent);
    c.setAgentSpecificTerms(new ArrayList<AgentSpecificTerm>());
    List<AgentSpecificTerm> agentSpecificTerms = new ArrayList<AgentSpecificTerm>();
    agentSpecificTerms = service.getListByAgent(agent.getId());
    Collections.sort(agentSpecificTerms, new AgentSpecificTermSorter());
    c.getAgentSpecificTerms().addAll(agentSpecificTerms);
    c.takeExpectednessSnapshot();

    // need to determine the category of the first element in the previous list
    if (c.getAgentSpecificTerms().size() > 0) {
        AgentSpecificTerm at = c.getAgentSpecificTerms().get(0);
        if (at instanceof AgentSpecificCtcTerm) {
            AgentSpecificCtcTerm t = (AgentSpecificCtcTerm) at;
            c.setTerminology(Term.CTC);
            c.setCtcVersion(ctcDao.getById(t.getTerm().getCategory().getCtc().getId()));
            // c.setMeddraVersion(meddraVersionDao.getById(10));
        } else {
            AgentSpecificMeddraLowLevelTerm t = (AgentSpecificMeddraLowLevelTerm) at;
            c.setTerminology(Term.MEDDRA);
            c.setMeddraVersion(meddraVersionDao.getById(t.getTerm().getMeddraVersion().getId()));
        }
    } else if (WebUtils.hasSubmitParameter(request, "terminologyName")) {
        c.setTerminology(Term.valueOf(request.getParameter("terminologyName")));
        c.setCtcVersion(ctcDao.getById(Integer.parseInt(request.getParameter("terminologyId"))));
    }

    if (WebUtils.hasSubmitParameter(request, "showSuccessMessage")) {
        request.setAttribute("flashMessage", "Information saved successfully");
    }

    return c;
}

From source file:Main.java

/**
 * Sorts <code>source</code> and adds the first n entries to
 * <code>dest</code>. If <code>source</code> contains less than n entries,
 * all of them are added to <code>dest</code>.
 * /*from   w  w w  . java 2s .c  o m*/
 * If adding an entry to <code>dest</code> does not increase the
 * collection's size, for example if <code>dest</code> is a set and already
 * contained the inserted contact, an additional entry of
 * <code>source</code> will be added, if available. This guarantees that
 * <code>n</code> new, distinct entries are added to collection
 * <code>dest</code> as long as this can be fulfilled with the contents of
 * <code>source</code>, and as <code>dest</code> does recognise duplicate
 * entries. Consequently, this guarantee does not hold for simple lists.
 * 
 * Both collections may not be <code>null</code>.
 * 
 * @param <T>
 *            the entry type of the collections.
 * @param source
 *            the source collection.
 * @param dest
 *            the destination collection.
 * @param order
 *            the order in which <code>source</code> is to be sorted.
 * @param n
 *            the number of new entries that are to be added to
 *            <code>dest</code>.
 */
public static <T> void copyNSorted(final Collection<? extends T> source, final Collection<? super T> dest,
        final Comparator<? super T> order, final int n) {
    final List<? extends T> src = Collections.list(Collections.enumeration(source));
    Collections.sort(src, order);
    final Iterator<? extends T> it = src.iterator();
    final int maxEntries = dest.size() + n;
    while (it.hasNext() && dest.size() < maxEntries) {
        dest.add(it.next());
    }
}

From source file:com.atlassian.connector.commons.misc.IntRanges.java

/**
 * @param ranges list cannot be empty. Copy is made here, so you may freely modify the array afterwards
 *///from  www  . j a  va 2  s.  c  o  m
public IntRanges(@NotNull List<IntRange> ranges) {
    this.ranges = new ArrayList<IntRange>(ranges);
    if (this.ranges.isEmpty()) {
        throw new IllegalArgumentException("Cannot create ranges object from the empty list");
    }
    Collections.sort(this.ranges, COMPARATOR);
}

From source file:models.TimelineItemTest.java

@Test
public void asc() {
    // When
    Collections.sort(list, TimelineItem.ASC);

    // Then
    assertThat(list).containsExactly(first, second, third);
}

From source file:org.synyx.hera.core.support.BeanListFactoryBean.java

public Object getObject() {

    List<T> beans = getBeans();
    Collections.sort(beans, COMPARATOR);

    return beans;
}