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:Main.java

/**
 * //from   w w w  .jav a2 s .c  o m
 * @param <T>
 * @param list
 * @param comparator
 * @return
 */
public static <T> T getLowerMiddleValue(List<T> list, Comparator<T> comparator) {
    if (null == list || list.isEmpty())
        return null;
    Collections.sort(list, comparator);
    return list.get((list.size() - 1) / 2);
}

From source file:Main.java

/**
 * Creates a new sorted map, based on the values from the given map and Comparator.
 * //  w w  w  .  j a  v  a 2s .  c  om
 * @param map the map which needs to be sorted
 * @param valueComparator the Comparator
 * @return a new sorted map
 */
public static <K, V> Map<K, V> sortMapByValue(Map<K, V> map, Comparator<Entry<K, V>> valueComparator) {
    if (map == null) {
        return Collections.emptyMap();
    }

    List<Entry<K, V>> entriesList = new LinkedList<>(map.entrySet());

    // Sort based on the map's values
    Collections.sort(entriesList, valueComparator);

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

From source file:Main.java

public static <T> ArrayList<T> toSortedArrayList(Iterable<? extends T> i, Comparator<? super T> comparator) {
    ArrayList<T> result = toArrayList(i);
    Collections.sort(result, comparator);
    return result;
}

From source file:de.micromata.genome.gwiki.plugin.rogmp3_1_0.RecBase.java

public static <T extends RecBase> void sortByIdx(List<T> list, final int idx, boolean desc) {
    if (desc == true) {
        Collections.sort(list, new Comparator<T>() {

            @Override/*w  w  w .  j  av  a  2s  . co m*/
            public int compare(T o1, T o2) {
                return o2.get(idx).compareTo(o1.get(idx));
            }
        });
    } else {
        Collections.sort(list, new Comparator<T>() {

            @Override
            public int compare(T o1, T o2) {
                return o1.get(idx).compareTo(o2.get(idx));
            }
        });
    }
}

From source file:Main.java

/** Returns an alphabetically sorted list of the keys. */
public static Vector getSortedKeyList(Hashtable hashtable) {
    Vector result = new Vector();
    Enumeration keys = hashtable.keys();
    while (keys.hasMoreElements()) {
        result.add(keys.nextElement());//w  w w.  j  a  v  a 2 s.c o  m
    }
    Collections.sort(result, new Comparator() {
        public int compare(Object a, Object b) {
            String textA = a.toString();
            String textB = b.toString();

            return textA.compareToIgnoreCase(textB);
        }
    });

    return result;
}

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

public static List<Tribe> filterTribes(List<Tribe> input, final String pFilter, Comparator<Tribe> pComparator) {
    if (pFilter == null) {
        return new ArrayList<>();
    }//from  w w  w.  ja  v  a2 s.  c om
    final String filter = pFilter.toLowerCase();

    if (filter.length() > 0) {
        CollectionUtils.filter(input, new Predicate() {
            @Override
            public boolean evaluate(Object o) {
                return ((Tribe) o).getName().toLowerCase().contains(filter);
            }
        });
    }

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

From source file:net.rrm.ehour.project.util.ProjectUtil.java

private static List<Project> filterBillability(Collection<Project> projects, boolean billable) {
    List<Project> sortedProjects = new ArrayList<>();

    for (Project project : projects) {
        if (project.isBillable() == billable) {
            sortedProjects.add(project);
        }/* w  w w .  j a  va 2  s. c  o m*/
    }

    Collections.sort(sortedProjects, new Comparator<Project>() {
        @Override
        public int compare(Project o1, Project o2) {
            return new CompareToBuilder().append(o1.getCustomer(), o2.getCustomer())
                    .append(o1.getName(), o2.getName()).append(o1.getProjectCode(), o2.getProjectCode())
                    .append(o1.getProjectId(), o2.getProjectId()).toComparison();
        }
    });
    return sortedProjects;
}

From source file:SortedProperties.java

@Override
public synchronized Enumeration<Object> keys() {
    Vector<Object> keyList = new Vector<Object>(super.keySet());
    Collections.sort(keyList, comparator);
    return keyList.elements();
}

From source file:ch.ksfx.util.calc.AssetPriceSparser.java

public static List<Observation> sparseAssetPriceWithoutAveragingObservation(List<Observation> assetPrices,
        Integer sparsingSeconds) {
    Collections.sort(assetPrices, new ObservationDateComparator());

    Integer currentIndex = 0;// w  w  w  . j a va  2s .c om
    List<Observation> sparsedPrices = new ArrayList<Observation>();

    for (Observation ap : assetPrices) {
        if (sparsedPrices.isEmpty()) {
            sparsedPrices.add(ap);
        }

        if (DateUtils.addSeconds(sparsedPrices.get(currentIndex).getObservationTime(), sparsingSeconds)
                .before(ap.getObservationTime())) {
            sparsedPrices.add(ap);
            currentIndex++;
        }
    }

    return sparsedPrices;
}

From source file:Main.java

public static Point findBestPreviewSizeValue(Camera.Parameters parameters, Point screenResolution) {

    List<Camera.Size> rawSupportedSizes = parameters.getSupportedPreviewSizes();
    if (rawSupportedSizes == null) {
        Log.w(TAG, "Device returned no supported preview sizes; using default");
        Camera.Size defaultSize = parameters.getPreviewSize();
        return new Point(defaultSize.width, defaultSize.height);
    }//from   w ww .j  a  va 2 s  .c  o  m

    // Sort by size, descending
    List<Camera.Size> supportedPreviewSizes = new ArrayList<Camera.Size>(rawSupportedSizes);
    Collections.sort(supportedPreviewSizes, new Comparator<Camera.Size>() {
        @Override
        public int compare(Camera.Size a, Camera.Size b) {
            int aPixels = a.height * a.width;
            int bPixels = b.height * b.width;
            if (bPixels < aPixels) {
                return -1;
            }
            if (bPixels > aPixels) {
                return 1;
            }
            return 0;
        }
    });

    if (Log.isLoggable(TAG, Log.INFO)) {
        StringBuilder previewSizesString = new StringBuilder();
        for (Camera.Size supportedPreviewSize : supportedPreviewSizes) {
            previewSizesString.append(supportedPreviewSize.width).append('x')
                    .append(supportedPreviewSize.height).append(' ');
        }
        Log.i(TAG, "Supported preview sizes: " + previewSizesString);
    }

    double screenAspectRatio = (double) screenResolution.x / (double) screenResolution.y;

    // Remove sizes that are unsuitable
    Iterator<Camera.Size> it = supportedPreviewSizes.iterator();
    while (it.hasNext()) {
        Camera.Size supportedPreviewSize = it.next();
        int realWidth = supportedPreviewSize.width;
        int realHeight = supportedPreviewSize.height;
        if (realWidth * realHeight < MIN_PREVIEW_PIXELS) {
            it.remove();
            continue;
        }

        boolean isCandidatePortrait = realWidth < realHeight;
        int maybeFlippedWidth = isCandidatePortrait ? realHeight : realWidth;
        int maybeFlippedHeight = isCandidatePortrait ? realWidth : realHeight;

        double aspectRatio = (double) maybeFlippedWidth / (double) maybeFlippedHeight;
        double distortion = Math.abs(aspectRatio - screenAspectRatio);
        if (distortion > MAX_ASPECT_DISTORTION) {
            it.remove();
            continue;
        }

        if (maybeFlippedWidth == screenResolution.x && maybeFlippedHeight == screenResolution.y) {
            Point exactPoint = new Point(realWidth, realHeight);
            Log.i(TAG, "Found preview size exactly matching screen size: " + exactPoint);
            return exactPoint;
        }
    }

    // If no exact match, use largest preview size. This was not a great
    // idea on older devices because
    // of the additional computation needed. We're likely to get here on
    // newer Android 4+ devices, where
    // the CPU is much more powerful.
    if (!supportedPreviewSizes.isEmpty()) {
        Camera.Size largestPreview = supportedPreviewSizes.get(0);
        Point largestSize = new Point(largestPreview.width, largestPreview.height);
        Log.i(TAG, "Using largest suitable preview size: " + largestSize);
        return largestSize;
    }

    // If there is nothing at all suitable, return current preview size
    Camera.Size defaultPreview = parameters.getPreviewSize();
    Point defaultSize = new Point(defaultPreview.width, defaultPreview.height);
    Log.i(TAG, "No suitable preview sizes, using default: " + defaultSize);

    return defaultSize;
}