Example usage for java.util Comparator Comparator

List of usage examples for java.util Comparator Comparator

Introduction

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

Prototype

Comparator

Source Link

Usage

From source file:com.rapidminer.operator.learner.tree.SelectionCreator.java

/**
 * Creates an example index start selection for each numerical attribute, or if there is none,
 * only one.//from   ww  w .jav a2  s  .c o  m
 *
 * @return a map containing for each numerical attribute an example index array such that the
 *         associated attribute values are in ascending order.
 */
public Map<Integer, int[]> getStartSelection() {
    Map<Integer, int[]> selection = new HashMap<>();
    if (columnTable.getNumberOfRegularNumericalAttributes() == 0) {
        selection.put(0, createFullArray(columnTable.getNumberOfExamples()));
    } else {
        Integer[] bigSelectionArray = createFullBigArray(columnTable.getNumberOfExamples());
        for (int j = columnTable.getNumberOfRegularNominalAttributes(); j < columnTable
                .getTotalNumberOfRegularAttributes(); j++) {
            final double[] attributeColumn = columnTable.getNumericalAttributeColumn(j);
            Integer[] startSelection = Arrays.copyOf(bigSelectionArray, bigSelectionArray.length);
            Arrays.sort(startSelection, new Comparator<Integer>() {

                @Override
                public int compare(Integer a, Integer b) {
                    return Double.compare(attributeColumn[a], attributeColumn[b]);
                }
            });
            selection.put(j, ArrayUtils.toPrimitive(startSelection));
        }
    }
    return selection;
}

From source file:com.github.fauu.natrank.model.entity.Country.java

@JsonIgnore
public Flag getLastFlag() {
    List<Flag> flags = new LinkedList<>(getFlags());

    Collections.sort(flags, new Comparator<Flag>() {
        @Override/*from w  w w  . j  a v a  2  s  . com*/
        public int compare(Flag flag1, Flag flag2) {
            return flag1.getPeriod().getFromDate().compareTo(flag2.getPeriod().getFromDate());
        }
    });

    return flags.size() > 0 ? flags.get(flags.size() - 1) : null;
}

From source file:fr.gotorennes.remote.RelayParkService.java

public List<RelayPark> getProximityRelayParks(final double latitude, final double longitude) {
    List<RelayPark> relayParks = loadList(getProximityCommand(latitude, longitude), NODE);
    if (relayParks != null) {
        Collections.sort(relayParks, new Comparator<RelayPark>() {

            @Override/*from  w  w  w.  j  av a 2 s  .  c o  m*/
            public int compare(RelayPark relayPark1, RelayPark relayPark2) {
                double distance1 = AbstractMapActivity.getDistance(latitude, longitude, relayPark1.latitude,
                        relayPark1.longitude);
                double distance2 = AbstractMapActivity.getDistance(latitude, longitude, relayPark2.latitude,
                        relayPark2.longitude);
                return Double.compare(distance1, distance2);
            }
        });
    }
    return relayParks;
}

From source file:org.fhcrc.cpl.viewer.ms2.gui.MS2ScanViewer.java

protected void buildChart() {
    if (scanInViewer == null)
        return;//from w  ww  . j  a  va 2s.c om
    if (chartInViewer != null) {
        remove(chartInViewer);
    }
    float[][] spectrum = scanInViewer.getSpectrum();
    float[] mzValues = new float[spectrum[0].length];
    float[] intensityValues = new float[spectrum[0].length];

    for (int i = 0; i < mzValues.length; i++) {
        mzValues[i] = spectrum[0][i];
        intensityValues[i] = spectrum[1][i];
    }

    chartInViewer = new PanelWithPeakChart(mzValues, intensityValues, "ms2scan");

    //add labels for highest peaks
    if (numHighestPeaksToLabel > 0) {
        XYPlot xyPlot = chartInViewer.getChart().getXYPlot();
        List<Pair<Float, Float>> peaksAsPairs = new ArrayList<Pair<Float, Float>>();
        for (int i = 0; i < mzValues.length; i++)
            peaksAsPairs.add(new Pair<Float, Float>(mzValues[i], intensityValues[i]));
        Collections.sort(peaksAsPairs, new Comparator<Pair<Float, Float>>() {
            @Override
            public int compare(Pair<Float, Float> pair1, Pair<Float, Float> pair2) {
                float diff = pair1.second - pair2.second;
                if (diff > 0)
                    return -1;
                if (diff < 0)
                    return 1;
                return 0;
            }
        });
        for (int i = 0; i < numHighestPeaksToLabel; i++) {
            Pair<Float, Float> pair = peaksAsPairs.get(i);
            xyPlot.addAnnotation(
                    new XYTextAnnotation("" + Rounder.round(pair.first, 3), pair.first, pair.second));
        }
    }

    add(chartInViewer);
    updateUI();
}

From source file:com.igormaznitsa.jute.Utils.java

public static List<TestContainer> sortDetectedClassMethodsForNameAndOrder(
        final List<TestContainer> testMethods) {
    Collections.sort(testMethods, new Comparator<TestContainer>() {
        @Override/*from w  ww  .j av  a  2s .c  o m*/
        public int compare(final TestContainer o1, final TestContainer o2) {
            final int order1 = o1.getOrder() < 0 ? -1 : o1.getOrder();
            final int order2 = o2.getOrder() < 0 ? -1 : o2.getOrder();

            final String name1 = o1.getMethodName();
            final String name2 = o2.getMethodName();

            final int result;
            if (order1 == order2) {
                result = name1.compareTo(name2);
            } else {
                result = Integer.compare(order1, order2);
            }
            return result;
        }
    });
    return testMethods;
}

From source file:com.openshift.internal.restclient.model.Service.java

private List<IServicePort> getPorts(boolean modifiable) {
    List<IServicePort> ports = new ArrayList<>();
    if (!get(SERVICE_PORT).isDefined())
        return ports;
    for (ModelNode node : get(SERVICE_PORT).asList()) {
        ports.add(new ServicePort(node));
    }//from w  ww .  jav  a2  s. c om
    Collections.sort(ports, new Comparator<IServicePort>() {
        @Override
        public int compare(IServicePort first, IServicePort second) {
            Integer port0 = first.getPort();
            Integer port1 = second.getPort();
            return port0.compareTo(port1);
        }
    });
    return modifiable ? ports : Collections.unmodifiableList(ports);
}

From source file:me.geenee.conf.SampleDataConfiguration.java

/**
 * Generates random set of tags./*from  ww  w . j  av a2  s.  c om*/
 * @return Set of tags.
 * @checkstyle ParameterNameCheck (25 lines)
 */
private Set<Tag> randomTags() {
    final int amount = this.random(1, TAGS_AMOUNT);
    final Set<Tag> result = new TreeSet<>(new Comparator<Tag>() {
        @Override
        public int compare(final Tag o1, final Tag o2) {
            Long first = Long.MIN_VALUE;
            Long second = Long.MIN_VALUE;
            if (o1 != null) {
                first = o1.getId();
            }
            if (o2 != null) {
                second = o2.getId();
            }
            return first.compareTo(second);
        }
    });
    for (int idx = 0; idx < amount; ++idx) {
        result.add(this.tags.findOne((long) this.random(1, TAGS_AMOUNT)));
    }
    return result;
}

From source file:com.googlecode.l10nmavenplugin.model.BundlePropertiesFile.java

/**
 * {@inheritDoc}//from   w w  w .j a  v a2  s.  c  o m
 */
public Set<String> getDuplicatedResourceKeys() {
    Set<String> duplicatedResourceKeys = new HashSet<String>();

    List<Map.Entry<String, String>> list = new ArrayList(properties.entrySet());
    // Sort the list by values, to be able to detect duplicates
    Collections.sort(list, new Comparator<Map.Entry<String, String>>() {
        public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
            return o1.getValue().compareTo(o2.getValue());
        }
    });

    String previousValue = null;
    for (Map.Entry<String, String> entry : list) {
        if (entry.getValue().equals(previousValue)) {
            duplicatedResourceKeys.add(entry.getKey());
        }
        previousValue = entry.getValue();
    }

    return duplicatedResourceKeys;
}

From source file:com.stratio.cassandra.index.query.SortField.java

/**
 * Returns a Java {@link Comparator} for {@link Columns} with the same logic as this {@link SortField}.
 *
 * @return A Java {@link Comparator} for {@link Columns} with the same logic as this {@link SortField}.
 *///from w w  w . j  av  a 2  s . c o m
public Comparator<Columns> comparator() {
    return new Comparator<Columns>() {
        public int compare(Columns o1, Columns o2) {

            if (o1 == null) {
                return o2 == null ? 0 : 1;
            }
            if (o2 == null) {
                return -1;
            }

            Column column1 = o1.getColumn(field);
            Column column2 = o2.getColumn(field);

            if (column1 == null) {
                return column2 == null ? 0 : 1;
            }
            if (column2 == null) {
                return -1;
            }

            AbstractType<?> type = column1.getType();
            ByteBuffer value1 = column1.getRawValue();
            ByteBuffer value2 = column2.getRawValue();
            return reverse ? type.compare(value2, value1) : type.compare(value1, value2);
        }
    };
}

From source file:biz.webgate.dominoext.poi.component.kernel.CSVProcessor.java

public ByteArrayOutputStream generateCSV(UICSV csvDef, FacesContext context) throws IOException, POIException {
    ByteArrayOutputStream csvBAOS = new ByteArrayOutputStream();
    OutputStreamWriter csvWriter = new OutputStreamWriter(csvBAOS);
    CSVPrinter csvPrinter = new CSVPrinter(csvWriter, CSVFormat.DEFAULT);

    List<CSVColumn> lstColumns = csvDef.getColumns();
    Collections.sort(lstColumns, new Comparator<CSVColumn>() {

        public int compare(CSVColumn o1, CSVColumn o2) {
            Integer p1 = Integer.valueOf(o1.getPosition());
            Integer p2 = Integer.valueOf(o2.getPosition());
            return p1.compareTo(p2);
        }//from   w w w  .j a v  a  2 s .com

    });
    if (csvDef.isIncludeHeader()) {
        for (CSVColumn cl : lstColumns) {
            csvPrinter.print(cl.getTitle());
        }
        csvPrinter.println();
    }

    // DATASOURCE holen und verarbeiten.
    if (csvDef.getDataSource() != null) {
        EmbeddedDataSourceExportProcessor.getInstance().process(lstColumns, csvDef, csvPrinter, context);
    } else {
        XPagesDataSourceExportProcessor.getInstance().process(lstColumns, csvDef, csvPrinter, context);
    }

    csvPrinter.flush();
    return csvBAOS;
}