Example usage for java.util Arrays sort

List of usage examples for java.util Arrays sort

Introduction

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

Prototype

public static void sort(Object[] a) 

Source Link

Document

Sorts the specified array of objects into ascending order, according to the Comparable natural ordering of its elements.

Usage

From source file:net.pernek.WordPair.java

@Override
public int hashCode() {
    // to get order invariant representation first order
    // the keywords alphabetically
    String[] keywords = { keyword1, keyword2 };
    Arrays.sort(keywords);

    return new HashCodeBuilder(17, 31).append(keywords[0]).append(keywords[1]).append(topic).toHashCode();
}

From source file:com.adguard.commons.utils.ReservedDomains.java

/**
 * Initializes/*from  ww  w  . jav  a  2 s . c  o m*/
 */
private static synchronized void initialize() {
    try {
        if (reservedDomainNames != null) {
            // Double check
            return;
        }

        LOG.info("Initialize ReservedDomains object");
        InputStream inputStream = ReservedDomains.class.getResourceAsStream("/effective_tld_names.dat");
        InputStreamReader reader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(reader);

        List<String> domains = new ArrayList<>();
        String line;

        while ((line = bufferedReader.readLine()) != null) {
            line = line.trim();

            if (!StringUtils.isBlank(line)) {
                domains.add(line);
            }
        }

        reservedDomainNames = new String[domains.size()];
        domains.toArray(reservedDomainNames);
        Arrays.sort(reservedDomainNames);

        LOG.info("ReservedDomains object has been initialized");
    } catch (Exception ex) {
        throw new RuntimeException("Cannot initialize reserved domains collection", ex);
    }
}

From source file:com.adaptris.security.util.SecurityUtil.java

public static String getAlgorithms() {
    initialise();//  w  w w .  j ava2s. c o  m
    StringBuffer sb = new StringBuffer("Security Algorithms available");
    for (Provider provider : Security.getProviders()) {
        sb.append("\nProvider : " + provider.toString() + "\n");

        Object[] objs = provider.keySet().toArray();
        Arrays.sort(objs);
        for (Object o : objs) {
            sb.append(o.toString() + ", ");
        }
    }
    return sb.toString();
}

From source file:com.jaspersoft.studio.swt.widgets.WLocale.java

public static String[] getLocales() {
    if (locales == null) {
        locales = Locale.getAvailableLocales();
        strLocales = new String[locales.length];
        for (int i = 0; i < strLocales.length; i++)
            strLocales[i] = locales[i].getDisplayName();
        Arrays.sort(strLocales);
    }/*from ww  w. j a  v  a 2  s  .com*/
    return strLocales;
}

From source file:Util.java

public static double median(double[] values) {
    double[] v = new double[values.length];
    double median = Double.NaN;
    if (v == null || v.length == 0) {
        throw new IllegalArgumentException("The data array either is null or does not contain any data.");
    } else if (v.length == 1) {
        median = v[0];/*from   w w  w.  j a v a2  s  .  c  o m*/
    } else {
        System.arraycopy(values, 0, v, 0, values.length);
        Arrays.sort(v);
        if (isEven(v.length)) {
            int i = (int) Math.ceil(v.length / 2D);
            double n1 = v[i];
            double n0 = v[i - 1];
            median = (n0 + n1) / 2;
        } else {
            median = v[v.length / 2];
        }
    }

    return median;

}

From source file:de.micromata.genome.gwiki.utils.StringUtils.java

public static Pair<Integer, String> indexOfAny(String text, int offset, String[] search) {
    if (text == null) {
        return INDEX_OF_ANY_NOT_FOUND_PAIR;
    }/*  w ww  . j  ava 2  s. c o m*/
    int[] idxe = new int[search.length];
    for (int i = 0; i < search.length; ++i) {
        if (search[i] == null) {
            idxe[i] = -1;
        } else {
            idxe[i] = text.indexOf(search[i], offset);
        }
    }
    Arrays.sort(idxe);
    for (int i = 0; i < idxe.length; ++i) {
        if (idxe[i] != -1) {
            return Pair.make(idxe[i], search[i]);
        }
    }
    return INDEX_OF_ANY_NOT_FOUND_PAIR;
}

From source file:com.cenrise.test.azkaban.PropsUtils.java

/**
 * Load job schedules from the given directories
 *
 * @param parent The parent properties for these properties
 * @param dir The directory to look in/*from  w  ww. j  av a  2  s  .c o m*/
 * @param suffixes File suffixes to load
 * @return The loaded set of schedules
 */
public static Props loadPropsInDir(final Props parent, final File dir, final String... suffixes) {
    try {
        final Props props = new Props(parent);
        final File[] files = dir.listFiles();
        Arrays.sort(files);
        if (files != null) {
            for (final File f : files) {
                if (f.isFile() && endsWith(f, suffixes)) {
                    props.putAll(new Props(null, f.getAbsolutePath()));
                }
            }
        }
        return props;
    } catch (final IOException e) {
        throw new RuntimeException("Error loading properties.", e);
    }
}

From source file:bigdataproject.KDistances.java

void getKSortedNearestNeighbors(int k) {
    double[] allK = new double[k * samples.length];
    int index = 0;
    for (double[] distRow : distanceMatrix) {
        Arrays.sort(distRow);
        double[] subK = Arrays.copyOfRange(distRow, 1, k);
        for (int j = 0; j < subK.length; j++) {
            final double value = subK[j];
            if (!DoubleStream.of(allK).anyMatch(x -> x == value)) {
                allK[index++] = value;//  ww w.  j a v a  2 s . co m
            }
        }
    }
    double[] finalArray = Arrays.copyOfRange(allK, 0, index);
    Arrays.sort(finalArray);
    Karray = finalArray;
}

From source file:javatranslation.matlab.dfa.java

public static void dfafunction(double[] tau, double MinBox, double MaxBox, int DFAorder) {

    double[] incoef = null;
    double[] l = null;
    l = new double[50];
    incoef = new double[50];
    for (int i = 0; i < 50; i++) {
        l = logspace(MinBox, MaxBox);/*from   ww  w  .  j  a v a  2 s . c o  m*/
        //System.out.println(l[i]);
        incoef[i] = Math.round(l[i]);
    }
    double xx = mean(tau);
    for (int i = 0; i < tau.length; i++) {
        tau[i] = tau[i] - xx;
    }
    double[] Y = cumsum(tau, dfa.mean(tau));
    double maxnumseg = incoef.length;
    double[] winlen = null;
    winlen = new double[50];
    for (int truta = 0; truta < 50; truta++) {
        winlen[truta] = incoef[truta];
    }
    Arrays.sort(winlen);
    ArrayUtils.reverse(winlen);

    double Ylength = Y.length;
    double F[][] = new double[(int) maxnumseg][1];
    for (int k = 0; k < maxnumseg; k++) {
        F[k][0] = 0;
    }
    double[] timevec = new double[50];
    for (int kk = 0; kk < maxnumseg; kk++) {
        timevec[kk] = winlen[kk];
        double numsegm = Math.floor(Ylength / winlen[kk]);
        double tempY[][] = new double[1][(int) numsegm];
        for (int k = 0; k < numsegm; k++) {
            tempY[0][k] = 0;
        }
        for (int zz = 0; zz < numsegm; zz++) {
            double overflowtest = zz * winlen[kk];
            if (overflowtest <= Ylength) {
                double[] tempvec = null;
                int ko = 0;
                for (double p = ((zz - 1) * winlen[kk] + 1); p <= (zz
                        * winlen[kk]); p = ((zz - 1) * winlen[kk] + 1) + 1) { // there are some errors in this loop
                    tempvec[ko] = Y[(int) p];
                    System.out.println(tempvec[(int) p]);
                    ko++;
                }
                //double temppol = polyfit(timevec,tempvec,DFAorder);
                MultivariateVectorOptimizer optimizer;
                optimizer = null;
                PolynomialFitter x = new PolynomialFitter(optimizer); // error here too
                double[] temppol = x.fit(DFAorder, timevec);

                double[] temppol2 = x.fit(DFAorder, tempvec);
                double[] arrayOfCoefficients = new double[temppol2.length];
                arrayOfCoefficients = temppol2;
                int len = arrayOfCoefficients.length;
                double retAnswer = 0;
                double ret = 0;
                for (int i = 0; i < len; i++) {
                    retAnswer = retAnswer + Math.pow(arrayOfCoefficients[i], i);
                }
                for (int i = 0; i < tempvec.length; i++) {
                    ret = tempvec[i] - (Math.pow(retAnswer, 2));
                }
                tempY[0][zz] = ((ret) / winlen[kk]);
            }
        }
        int k;
        double[] kopp = new double[(int) maxnumseg];
        for (k = 0; k < maxnumseg; k++) {
            kopp[k] = tempY[0][k];
        }
        double nonzerotempY = dfa.sum2(kopp);
        F[kk][0] = Math.sqrt(dfa.sum(kopp) / nonzerotempY);

    }
    double[] LF;
    LF = new double[(int) maxnumseg];
    double[] LN;
    LN = new double[(int) maxnumseg];
    for (int i = 0; i < maxnumseg; i++) {
        LF[i] = Math.log10(F[i][0]);
        LN[i] = Math.log10(winlen[i]);
    }
    double[][] XLN = new double[LN.length][LN.length];
    for (int i = 0; i < LN.length; i++) {
        XLN[i][0] = 1;
        XLN[i][1] = LN[i];
    }
    SimpleRegression x;
    x = new SimpleRegression();
    x.addObservations(XLN, LF); // and probably error in this one too
    RegressionResults b = x.regress();
    // System.out.println(b.getParameterEstimates());

    //double LF=Math.log10(F); % log fluctuations
    //double LN=Math.log10(winlen);
}

From source file:edu.umn.msi.tropix.proteomics.itraqquantitation.impl.Variance.java

static double[][] createVarianceMatrix(final List<ITraqLabel> labels,
        final Map<ITraqLabel, double[]> intensities, final double[] ds, final int binSize) {
    final int n = intensities.values().iterator().next().length;
    final double[] reference = new double[n];
    for (int i = 0; i < n; i++) {
        double intensitiesProduct = 1.0d;
        for (final ITraqLabel label : labels) {
            final double intensity = intensities.get(label)[i];
            intensitiesProduct *= intensity;
        }//from w w  w  .j av a2 s.  c o m
        reference[i] = MathUtils.log(2.0, intensitiesProduct);
    }

    // final double[] sortedReference = Arrays.copyOf(reference, reference.length);
    final double[] sortedReference = new double[reference.length];
    for (int i = 0; i < reference.length; i++) {
        sortedReference[i] = reference[i];
    }

    Arrays.sort(sortedReference);

    final List<ITraqRatio> ratios = ITraqLabels.buildRatios(labels);
    final int numRatios = ratios.size();
    final int numRows = numRatios * n;

    // Precompute ratios
    final double[] actualRatios = new double[numRatios];
    final double[][] numerators = new double[numRatios][];
    final double[][] denominators = new double[numRatios][];
    for (int ratioIndex = 0; ratioIndex < numRatios; ratioIndex++) {
        final ITraqRatio ratio = ratios.get(ratioIndex);
        final int numeratorIndex = labels.indexOf(ratio.getNumerator());
        final int denominatorIndex = labels.indexOf(ratio.getDenominator());

        numerators[ratioIndex] = intensities.get(ratio.getNumerator());
        denominators[ratioIndex] = intensities.get(ratio.getDenominator());
        actualRatios[ratioIndex] = MathUtils.log(2.0, ds[numeratorIndex] / ds[denominatorIndex]);
    }

    final double[][] samplePoints = new double[numRows][2];
    for (int sortedReferenceIndex = 0; sortedReferenceIndex < n; sortedReferenceIndex++) {
        final double referenceValue = sortedReference[sortedReferenceIndex];
        final int referenceIndex = indexOf(referenceValue, reference);
        final int rowOffset = sortedReferenceIndex * numRatios;
        for (int ratioIndex = 0; ratioIndex < numRatios; ratioIndex++) {
            final double actualRatio = actualRatios[ratioIndex];
            final double estimatedRatio = MathUtils.log(2.0,
                    numerators[ratioIndex][referenceIndex] / denominators[ratioIndex][referenceIndex]);
            final double diff = (estimatedRatio - actualRatio);
            samplePoints[rowOffset + ratioIndex][0] = referenceValue;
            samplePoints[rowOffset + ratioIndex][1] = diff * diff;
        }
    }

    final int numBins = numRows / binSize;
    final double[][] matrixXV = new double[numBins][2];

    double maxV = Double.MIN_VALUE;
    double sumX = 0.0, sumV = 0.0;
    int curBin = 0;
    for (int i = 0; i < numRows; i++) {
        sumX += samplePoints[i][0];
        sumV += samplePoints[i][1];
        if ((i + 1) % binSize == 0) {
            final double x = sumX / binSize;
            final double v = sumV / binSize;
            final double binWeight = 1 / v;
            if (!Double.isInfinite(binWeight)) {
                maxV = Math.max(binWeight, maxV);
            }

            matrixXV[curBin][0] = x;
            matrixXV[curBin][1] = binWeight;
            curBin++;

            sumX = 0.0;
            sumV = 0.0;
        }
    }

    for (int i = 0; i < numBins; i++) {
        if (Double.isInfinite(matrixXV[i][1])) {
            matrixXV[i][1] = maxV;
        }
    }

    for (int i = 0; i < numBins - 1; i++) {
        matrixXV[i][0] = (matrixXV[i][0] + matrixXV[i + 1][0]) / 2.0;
    }

    return matrixXV;
}