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:de.tudarmstadt.ukp.experiments.argumentation.clustering.entropy.MatrixExperiments.java

public static <K, V extends Comparable<? super V>> LinkedHashMap<K, V> sortByValue(Map<K, V> map) {
    List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
    Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
        @Override/*from  ww w.ja v a  2 s  .com*/
        public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
            return (o1.getValue()).compareTo(o2.getValue());
        }
    });

    LinkedHashMap<K, V> result = new LinkedHashMap<>();
    for (Map.Entry<K, V> entry : list) {
        result.put(entry.getKey(), entry.getValue());
    }
    return result;
}

From source file:com.codecrate.shard.dice.DropDice.java

public int roll() {
    List values = dice.rollIterations();

    //sort from high to low 
    Collections.sort(values, new ReverseComparator());

    int value = 0;
    for (int x = 0; x < dice.getIterations() - drop; x++) {
        value += ((Integer) values.get(x)).intValue();
    }//from  w  ww .  j  av a2s. c o m
    return value;
}

From source file:net.seedboxer.sources.processor.rss.sorter.RssSortProcessor.java

protected void sortEntries(List<SyndEntry> list) {
    Collections.sort(list, new RssDateComparator());
}

From source file:fr.mby.portal.coreimpl.auth.BasicAuthenticationManager.java

@PostConstruct
public void init() {
    // Sort the IAuthenticationProvider list by Annotation Order
    Collections.sort(this.internalAuthenticationProviders, AnnotationAwareOrderComparator.INSTANCE);
}

From source file:edu.buffalo.fusim.BackgroundGenerator.java

public List<FusionGene> generate(int nFusions, int genesPerFusion) {
    List<FusionGene> fusions = new ArrayList<FusionGene>();

    List<TranscriptRecord> transcripts = selector.select();
    if (transcripts.size() == 0)
        return fusions;

    Collections.sort(transcripts, new TranscriptCompare());

    if (GeneSelectionMethod.BINNED.equals(method)) {
        logger.info("Generating fusions using RPKM bins...");
        // First bin genes into RPKM buckets
        GeneBins geneBins = new GeneBins(nFusions);
        geneBins.fill(transcripts);/*  www. j ava2  s.c  o m*/

        for (int i = 0; i < geneBins.size(); i++) {
            IntArrayList b = geneBins.getBin(i);
            b.trimToSize();
            if (b.elements().length < genesPerFusion) {
                logger.fatal("Not enough genes in this bin to generate fusion!");
                continue;
            }
            List<TranscriptRecord> genes = new ArrayList<TranscriptRecord>();
            int[] sample = RandomSamplingAssistant.sampleArray(genesPerFusion, b.elements());
            for (int s = 0; s < sample.length; s++) {
                genes.add(transcripts.get(sample[s]));

                // Self-fusion
                if (sample.length == 1) {
                    genes.add(transcripts.get(sample[s]));
                }
            }
            fusions.add(new FusionGene(genes));
        }
    } else if (GeneSelectionMethod.EMPIRICAL.equals(method)
            || GeneSelectionMethod.EMPIRICAL_STURGES.equals(method)) {
        logger.info("Generating fusions based on empirical background distribution...");
        boolean sturges = false;
        if (GeneSelectionMethod.EMPIRICAL_STURGES.equals(method)) {
            logger.info("Using sturges method for computing bin sizes...");
            sturges = true;
        }

        // First bin genes into RPKM buckets
        EmpiricalGeneBins geneBins = new EmpiricalGeneBins();
        geneBins.fill(transcripts, sturges);

        Random r = new Random();
        int[] distribution = new int[transcripts.size()];
        int index = 0;
        for (int i = 0; i < geneBins.size(); i++) {
            IntArrayList b = geneBins.getBin(i);
            for (int j = 0; j < b.size(); j++) {
                distribution[index] = i;
                index++;
            }
        }

        for (int x = 0; x < nFusions; x++) {
            List<TranscriptRecord> genes = new ArrayList<TranscriptRecord>();

            for (int j = 0; j < genesPerFusion; j++) {
                int binIndex = distribution[r.nextInt(distribution.length)];
                IntArrayList b = geneBins.getBin(binIndex);
                TranscriptRecord tr = transcripts.get(b.get(r.nextInt(b.size())));
                genes.add(tr);

                // Self-fusion
                if (genesPerFusion == 1) {
                    genes.add(tr);
                }
            }
            fusions.add(new FusionGene(genes));
        }
    } else {
        logger.info("Generating fusions based on uniform distribution...");
        RandomGenerator rg = new RandomGenerator();
        rg.setGeneSelector(selector);
        rg.setFilters(filters);
        rg.setGeneSelectionMethod(method);
        fusions = rg.generate(nFusions, genesPerFusion);
    }

    return fusions;
}

From source file:net.sourceforge.fenixedu.presentationTier.renderers.providers.candidacy.DegreeTypeDegrees.java

@Override
@SuppressWarnings("unchecked")
public Object provide(Object source, Object currentValue) {

    final List<Degree> result = new ArrayList<Degree>();
    for (Degree degree : Degree.readNotEmptyDegrees()) {
        if (degree.getDegreeType() == DegreeType.BOLONHA_DEGREE) {
            result.add(degree);/*from  w  ww.ja  v a2 s. c  o m*/
        }
    }
    Collections.sort(result, new BeanComparator("name"));
    return result;
}

From source file:lydichris.smashbracket.services.BracketService.java

private List<Match> generateSingleElimationTournament(Tournament tournament) {
    List<Entrant> entrants = entrantService.getEntrantsInTournament(tournament.getId());
    Collections.sort(entrants, new SeedComparator());
    int entrantsSizeToFillWithByesUntil = 0;

    entrantsSizeToFillWithByesUntil = calculateHigherPowerOfTwo(entrants.size());

    while (entrants.size() < entrantsSizeToFillWithByesUntil) {
        entrants.add(bye);//from  w w w  .j av a  2s . c o  m
    }

    List<Match> rounds = generateFirstRounds(entrants, tournament);
    rounds.addAll(generateRemainingRounds(rounds, 1, tournament));
    return matchPersistence.persistMatches(rounds);
}

From source file:org.syncope.console.commons.RoleTreeBuilder.java

private List<RoleTO> getChildRoles(final long parentRoleId, final List<RoleTO> roles) {

    List<RoleTO> result = new ArrayList<RoleTO>();
    for (RoleTO role : roles) {
        if (role.getParent() == parentRoleId) {
            result.add(role);//from  w  ww. j  a va2s  .  c  o m
        }
    }

    Collections.sort(result, comparator);
    return result;
}

From source file:info.magnolia.importexport.BootstrapUtil.java

public static void bootstrap(String[] resourceNames, int importUUIDBehavior)
        throws IOException, RepositoryException {
    // sort by length --> import parent node first
    List<String> list = new ArrayList<String>(Arrays.asList(resourceNames));

    Collections.sort(list, new StringLengthComparator());

    for (Iterator<String> iter = list.iterator(); iter.hasNext();) {
        String resourceName = iter.next();

        String name = getFilenameFromResource(resourceName, ".xml");
        String repository = getWorkspaceNameFromResource(resourceName);
        String pathName = getPathnameFromResource(resourceName);
        String fullPath = getFullpathFromResource(resourceName);
        String nodeName = StringUtils.substringAfterLast(fullPath, "/");

        log.debug("Will bootstrap {}", resourceName);

        final InputStream stream = BootstrapUtil.class.getResourceAsStream(resourceName);
        if (stream == null) {
            throw new IOException("Can't find resource to bootstrap at " + resourceName);
        }//from w w  w.j a v a  2 s.  co m

        // if the node already exists we will keep the order
        String nameOfNodeAfterTheImportedNode = null;

        final HierarchyManager hm = MgnlContext.getHierarchyManager(repository);

        // if the path already exists --> delete it
        try {

            // HM can be null if module is not properly registered and the repository has not been created
            if (hm != null && hm.isExist(fullPath)) {
                // but keep the order
                Content node = hm.getContent(fullPath);
                SiblingsHelper siblings = SiblingsHelper.of(node);
                if (!siblings.isLast()) {
                    nameOfNodeAfterTheImportedNode = siblings.next().getName();
                }

                hm.delete(fullPath);
                log.warn("Deleted already existing node for bootstrapping: {}", fullPath);
            }
        } catch (RepositoryException e) {
            throw new RepositoryException("Can't check existence of node for bootstrap file: [" + name + "]",
                    e);
        }

        DataTransporter.importXmlStream(stream, repository, pathName, name, false, importUUIDBehavior, false,
                true);

        if (nameOfNodeAfterTheImportedNode != null) {
            Content newNode = hm.getContent(fullPath);
            newNode.getParent().orderBefore(nodeName, nameOfNodeAfterTheImportedNode);
        }

    }
}