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")
public static <T extends Comparable<? super T>> void sort(List<T> list) 

Source Link

Document

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

Usage

From source file:edu.umich.robot.util.Configs.java

public static void toLog(Log logger, Config config) {
    if (!logger.isDebugEnabled())
        return;/* ww w  .  j a v  a 2  s .co m*/

    List<String> keys = new LinkedList<String>();
    Collections.addAll(keys, config.getKeys());
    Collections.sort(keys);
    for (String key : keys)
        logger.debug(String.format("%s = %s", key, Arrays.toString(config.getStrings(key))));
}

From source file:com.vrem.wifianalyzer.settings.CountryPreference.java

@NonNull
private static List<Data> getData() {
    List<Data> results = new ArrayList<>(CollectionUtils.collect(WiFiChannelCountry.getAll(), new ToData()));
    Collections.sort(results);
    return results;
}

From source file:Main.java

private static String generateSignature(String base, String type, String nonce, String timestamp, String token,
        String tokenSecret, String verifier, ArrayList<String> parameters)
        throws NoSuchAlgorithmException, InvalidKeyException {
    String encodedBase = Uri.encode(base);

    StringBuilder builder = new StringBuilder();

    //Create an array of all the parameters
    //So that we can sort them
    //OAuth requires that we sort all parameters
    ArrayList<String> sortingArray = new ArrayList<String>();

    sortingArray.add("oauth_consumer_key=" + oauthConsumerKey);
    sortingArray.add("oauth_nonce=" + nonce);
    sortingArray.add("oauth_signature_method=HMAC-SHA1");
    sortingArray.add("oauth_timestamp=" + timestamp);
    sortingArray.add("oauth_version=1.0");

    if (parameters != null) {
        sortingArray.addAll(parameters);
    }/*from w ww  .j a v  a 2 s. c  om*/

    if (token != "" && token != null) {
        sortingArray.add("oauth_token=" + token);
    }
    if (verifier != "" && verifier != null) {
        sortingArray.add("oauth_verifier=" + verifier);
    }

    Collections.sort(sortingArray);

    //Append all parameters to the builder in the right order
    for (int i = 0; i < sortingArray.size(); i++) {
        if (i > 0)
            builder.append("&" + sortingArray.get(i));
        else
            builder.append(sortingArray.get(i));

    }

    String params = builder.toString();
    //Percent encoded the whole url
    String encodedParams = Uri.encode(params);

    String completeUrl = type + "&" + encodedBase + "&" + encodedParams;

    String completeSecret = oauthSecretKey;

    if (tokenSecret != null && tokenSecret != "") {
        completeSecret = completeSecret + "&" + tokenSecret;
    } else {
        completeSecret = completeSecret + "&";
    }

    Log.v("Complete URL: ", completeUrl);
    Log.v("Complete Key: ", completeSecret);

    Mac mac = Mac.getInstance("HmacSHA1");
    SecretKeySpec secret = new SecretKeySpec(completeSecret.getBytes(), mac.getAlgorithm());
    mac.init(secret);
    byte[] sig = mac.doFinal(completeUrl.getBytes());

    String signature = Base64.encodeToString(sig, 0);
    signature = signature.replace("+", "%2b"); //Specifically encode all +s to %2b

    return signature;
}

From source file:Main.java

public static List<String> stringToList(@Nullable String arrayString) {
    if (TextUtils.isEmpty(arrayString)) {
        return Collections.emptyList();
    }/* w  ww. j  ava2  s.co m*/
    final String[] splitted = arrayString.trim().split(" ");
    final ArrayList<String> list = new ArrayList<>(Arrays.asList(splitted));
    Collections.sort(list);
    return list;
}

From source file:Main.java

public static String[] getXmlFiles(String path) {
    List<String> xmlFiles = new ArrayList<>();
    ZipFile zipFile = null;//from w  w  w  .  j  a v  a  2 s .  c o  m
    try {
        zipFile = new ZipFile(path);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            String name = entry.getName();
            if (name.endsWith(".xml") && !name.equals("AndroidManifest.xml")) {
                xmlFiles.add(name);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException ignored) {
            }
        }
    }
    Collections.sort(xmlFiles);
    return xmlFiles.toArray(new String[xmlFiles.size()]);
}

From source file:Main.java

public Enumeration keys() {
    Enumeration keysEnum = super.keys();
    Vector<String> keyList = new Vector<String>();
    while (keysEnum.hasMoreElements()) {
        keyList.add((String) keysEnum.nextElement());
    }//ww w. jav  a2  s  . c o  m
    Collections.sort(keyList);
    return keyList.elements();
}

From source file:com.actelion.research.spiritcore.util.StatUtils.java

/**
 * Calculate Kruskal-Walis (http://en.wikipedia.org/wiki/Kruskal%E2%80%93Wallis_one-way_analysis_of_variance)
 * @param values//from w  w  w .  ja  v  a2 s  . c  om
 * @return
 */
public static double getKruskalWallis(List<double[]> values) {

    //Assign ranks
    assert values.size() > 1;

    List<Double> allDoubles = new ArrayList<>();
    for (double[] a : values) {
        assert a.length > 0;
        for (double d : a) {
            allDoubles.add(d);
        }
    }
    int N = allDoubles.size();
    Collections.sort(allDoubles);
    double[] allDoublesArray = new double[allDoubles.size()];
    for (int i = 0; i < allDoubles.size(); i++)
        allDoublesArray[i] = allDoubles.get(i);
    List<double[]> ranks = new ArrayList<double[]>();
    for (double[] a : values) {
        double[] rankArray = new double[a.length];
        ranks.add(rankArray);
        for (int i = 0; i < a.length; i++) {
            int r = Arrays.binarySearch(allDoublesArray, a[i]);
            assert r >= 0;
            int r1 = r, r2 = r;
            while (r1 > 0 && allDoublesArray[r1 - 1] == a[i])
                r1--;
            while (r2 < allDoublesArray.length - 1 && allDoublesArray[r2 + 1] == a[i])
                r2++;
            rankArray[i] = (r1 + r2) / 2.0 + 1;
        }
    }

    //Calculate rank average per group
    List<Double> rankSum = new ArrayList<Double>();
    for (double[] a : ranks) {
        double sum = 0;
        for (double d : a)
            sum += d;
        rankSum.add(sum);
    }

    double sum = 0;
    for (int i = 0; i < ranks.size(); i++) {
        sum += rankSum.get(i) * rankSum.get(i) / ranks.get(i).length;
    }
    double H = 12.0 / (N * (N + 1)) * sum - 3 * (N + 1);

    ChiSquaredDistribution chi = new ChiSquaredDistribution(values.size() - 1);
    double K = 1 - chi.cumulativeProbability(H);

    return K;
}

From source file:it.govpay.model.BasicModel.java

public static <T extends Comparable<? super T>> boolean equals(List<T> a, List<T> b) {
    if (a == null || b == null)
        return a == null && b == null;

    if (a.size() != b.size())
        return false;

    Collections.sort(a);
    Collections.sort(b);/*from  ww w . j  a v a  2s  . c  o  m*/

    for (int index = 0; index < a.size(); index++) {
        Object a1 = b.get(index);
        Object b1 = b.get(index);

        if (!a1.equals(b1))
            return false;
    }

    return true;
}

From source file:com.vmware.photon.controller.api.frontend.lib.UsageTagHelper.java

public static List<UsageTag> deserialize(String concatenatedUsageTags) {
    if (StringUtils.isBlank(concatenatedUsageTags)) {
        throw new IllegalArgumentException("Blank string cannot be deserialized to list of UsageTags");
    }/* w  w w.java 2 s.  c  o m*/

    try {
        List<UsageTag> usageTags = objectMapper.readValue(concatenatedUsageTags,
                new TypeReference<List<UsageTag>>() {
                });
        Collections.sort(usageTags);
        return usageTags;
    } catch (IOException e) {
        logger.error(String.format("Error deserializing UsageTag %s", concatenatedUsageTags), e);
        throw new IllegalArgumentException(
                String.format("Error deserializing UsageTag %s", concatenatedUsageTags), e);
    }
}

From source file:com.google.mr4c.sources.SourceUtils.java

public static void copySource(ArchiveSource src, ArchiveSource dest, boolean streamOutput) throws IOException {
    List<String> files = new ArrayList<String>(src.getAllFileNames());
    Collections.sort(files);
    dest.startWrite();/*from w w  w.j a va2 s  .co m*/
    for (String file : files) {
        DataFileSource fileSrc = src.getFileSource(file);
        DataFileSink fileSink = dest.getFileSink(file);
        copyFile(fileSrc, fileSink, streamOutput);
    }
    for (String file : src.getAllMetadataFileNames()) {
        DataFileSource fileSrc = src.getMetadataFileSource(file);
        DataFileSink fileSink = dest.getMetadataFileSink(file);
        copyFile(fileSrc, fileSink, streamOutput);
    }
    dest.finishWrite();
    src.close();
}