Java Set Sort sortLong( Collection> entrySet)

Here you can find the source of sortLong( Collection> entrySet)

Description

sort Long

License

Apache License

Return

a sorted list where the string with the highest integer value comes first!

Declaration

public static <T> List<Entry<T, Long>> sortLong(
        Collection<Entry<T, Long>> entrySet) 

Method Source Code

//package com.java2s;
/**//from   w w  w.  j av  a 2  s  . c o m
 * Copyright (C) 2010 Peter Karich <jetwick_@_pannous_._info>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;

import java.util.List;

import java.util.Map.Entry;

public class Main {
    /**
     * @return a sorted list where the string with the highest integer value comes first!
     */
    public static <T> List<Entry<T, Long>> sortLong(
            Collection<Entry<T, Long>> entrySet) {
        List<Entry<T, Long>> sorted = new ArrayList<Entry<T, Long>>(
                entrySet);
        Collections.sort(sorted, new Comparator<Entry<T, Long>>() {

            @Override
            public int compare(Entry<T, Long> o1, Entry<T, Long> o2) {
                long i1 = o1.getValue();
                long i2 = o2.getValue();
                if (i1 < i2)
                    return 1;
                else if (i1 > i2)
                    return -1;
                else
                    return 0;
            }
        });

        return sorted;
    }

    /**
     * @return a sorted list where the object with the highest integer value comes first!
     */
    public static <T> List<Entry<T, Integer>> sort(
            Collection<Entry<T, Integer>> entrySet) {
        List<Entry<T, Integer>> sorted = new ArrayList<Entry<T, Integer>>(
                entrySet);
        Collections.sort(sorted, new Comparator<Entry<T, Integer>>() {

            @Override
            public int compare(Entry<T, Integer> o1, Entry<T, Integer> o2) {
                int i1 = o1.getValue();
                int i2 = o2.getValue();
                if (i1 < i2)
                    return 1;
                else if (i1 > i2)
                    return -1;
                else
                    return 0;
            }
        });

        return sorted;
    }
}

Related

  1. sort(Set data)
  2. sortByToCloseOrder(Set set)
  3. sortedSetToString(SortedSet sortedSet)
  4. sortKeys(final Set keySet)
  5. sortKeys(Set unsortedKeys)