int[] aaa = { 12, 12, 123, 2, 234, 0, 2347 };
Arrays.sort(aaa);
for (Integer aa : aaa) {
System.out.println(aa);
}
List是没有倒序的方法的,但是可以通过下标的最大值去访问。
List<Integer> a = new ArrayList<>();
a.add(1);
a.add(6);
a.add(-90);
Collections.sort(a, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
// TODO 自动生成的方法存根
return o1.compareTo(o2);
}
});
for (Integer aa : a) {
System.out.println(aa);
}
//将list倒序
Collections.reverse(a);
其本质还是,将o1和o2两个对象的指定值进行比较,返回大于0,小于0,等于的0的int值。将数据进行排序。对int型无效。当然int型的话,直接相减就行。
//字符串,时间字符串的排序
List<String> stringList=new ArrayList();
// stringList.add("张三");
// stringList.add("李四");
// stringList.add("王五");
// stringList.add("张之");
// stringList.add("王七");
// stringList.add("李七");
stringList.add("2019-10-29 10:29:45");
stringList.add("2016-10-29 10:29:45");
stringList.add("2019-05-29 10:29:45");
Collections.sort(stringList,new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
// TODO 自动生成的方法存根
return -o1.compareTo(o2);//默认是升序,o1在前,o2在后
}
});
public static Map<Integer, Double> Probs = new TreeMap<Integer, Double>();
public static void main(String[] args) {
Probs.put(1, 0.5);
Probs.put(2, 1.5);
Probs.put(10, 0.2);
Probs.put(4, 10.2);
Probs= sortMapByKey(Probs); //按Key进行排序
System.out.println("基于key值的降序,排序输出结果为:");
for (Map.Entry<Integer, Double> entry : Probs.entrySet()) {
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
Probs = sortByValueDescending(Probs);
System.out.println("基于value值的降序,排序输出结果为:");
for (Map.Entry<Integer, Double> entry : Probs.entrySet()) {
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
System.out.println();
System.out.println("基于value值的升序,排序输出结果为:");
Probs = sortByValueAscending(Probs);//排序
for (Map.Entry<Integer, Double> entry : Probs.entrySet()) {
System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
}
}
public static <K, V extends Comparable<? super V>> Map<K, V> sortMapByKey(Map<K, V> map)
{
List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<K, V>>()
{
@Override
public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2)
{
int compare = ((Integer) o1.getKey()).compareTo((Integer) o2.getKey());
return -compare;
}
});
//重新赋值
Map<K, V> result = new LinkedHashMap<K, V>();
for (Map.Entry<K, V> entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}
//降序排序
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValueDescending(Map<K, V> map)
{
List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<K, V>>()
{
@Override
public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2)
{
int compare = (o1.getValue()).compareTo(o2.getValue());
return -compare;
}
});
//重新赋值
Map<K, V> result = new LinkedHashMap<K, V>();
for (Map.Entry<K, V> entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}
//升序排序
public static <K, V extends Comparable<? super V>> Map<K, V> sortByValueAscending(Map<K, V> map)
{
List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<K, V>>()
{
@Override
public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2)
{
int compare = (o1.getValue()).compareTo(o2.getValue());
return compare;
}
});
Map<K, V> result = new LinkedHashMap<K, V>();
for (Map.Entry<K, V> entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}