package ah;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
// Java降序不容易
// 无法对基本类型进行降序排列
public class TestJava升序降序 {
public static void printArr(String _msg, Integer _arr[]) {
System.out.println(_msg + " 输出数组:");
for (Integer i : _arr) {
System.out.print(i);
}
System.out.println();
}
public static void 升序() {
int _arr[] = { 5, 3, 1, 2, 4 };
// 类,或包装类
Arrays.sort(_arr);
System.out.println("升序 输出数组:");
for (int i : _arr) {
System.out.print(i);
}
System.out.println();
}
public static void 降序sort版() {
Integer _arr[] = { 5, 3, 1, 2, 4 };
// 类,或包装类
Arrays.sort(_arr, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
printArr("降序sort版", _arr);
}
public static void 降序转置版() {
Integer _arr[] = { 5, 3, 1, 2, 4 };
Arrays.sort(_arr);
List<Integer> asList = Arrays.asList(_arr);
// 类,或包装类
Collections.reverse(asList);
printArr("降序 转置版", _arr);
}
public static void main(String[] args) {
升序();
降序sort版();
降序转置版();
}
}
class DescComparator implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
}

Java中的升序和降序的更多相关文章

  1. Java排序之升序与降序

    以前在学校学排序的时候,总是自己写排序的代码,真正到工作中,直接使用java提供的排序方法,但最近发现行业默认的和学习时有些不一样. 以前总是在进行排序时如果前边的数字和后边数字的差为负则交换两个数字 ...

  2. mysql中的升序和降序以及一个字段升序和一个字段降序

    mySql中,升序为asc,降序为desc.例如: 升序:select   *  from  表名 order by  表中的字段 asc(mysql中默认是升序排列,可不写) 降序:select   ...

  3. 【Java】数组升序和降序

    int[] x={1,6,4,8,6,9,12,32,76,34,23}; 升序: Arrays.sort(x); 降序: resort(x); public int[] resort(int[] n ...

  4. 【转】java comparator 升序、降序、倒序从源码角度理解

    原文链接:https://blog.csdn.net/u013066244/article/details/78997869 环境jdk:1.7+ 前言之前我写过关于comparator的理解,但是都 ...

  5. Java的数组和list升序,降序,逆序函数Collections.sort和Arrays.sort的使用

    list升序,降序,逆序List<Integer>list =new ArrayList<Integer>();//如果list是 5 7 2 6 8 1 41.升序:Coll ...

  6. 通过orderby关键字,LINQ可以实现升序和降序排序。LINQ还支持次要排序。

    通过orderby关键字,LINQ可以实现升序和降序排序.LINQ还支持次要排序. LINQ默认的排序是升序排序,如果你想使用降序排序,就要使用descending关键字. static void M ...

  7. DataGridView使用技巧十三:点击列头实现升序和降序排序

    DataGridView 列有三种排序模式.每一列的排序模式是通过该列的 SortMode 属性指定的,该属性可以设置为以下的 DataGridViewColumnSortMode 枚举值之一. Da ...

  8. JDBC_10_使用Statement实现升序和降序

    使用Statement数据库操作对象实现升序和降序 Statement可以使用在需要SQL语句拼接的情况下,因为在这样的情况下如果使用PreparedStatement就会给需要拼接的某个SQL关键字 ...

  9. 转<<C#集合Dictionary中按值的降序排列

    转载地址:http://blog.sina.com.cn/s/blog_5c5bc9070100pped.html C#集合Dictionary中按值的降序排列 static void Main(st ...

随机推荐

  1. HQL知识点一

    Hive创表语法 create [external] table [if not exists] [db_name.]table_name (col1_name data_type,col2_name ...

  2. 转 Postman访问Webapi的Get/Post/Put/Delte请求

    Postman访问Webapi的Get/Post/Put/Delte请求 2018年07月26日 15:04:46 DoNotWorkOvertime 阅读数:348 标签: WebApiPostma ...

  3. Golang微服务:Micro Trace使用opentracing jaeger

    trace Micro通过Wrapper实现了三种trace接口,aswxray,opencensus,opentracing,这里主要关注opentracing,opentracing已成为行业标准 ...

  4. Fiddler实现手机抓包——小白入门

    方法1:https://www.cnblogs.com/hzg1981/p/5610530.html 方法2:转载自:http://blog.csdn.net/gld824125233/article ...

  5. zznuoj 1540 : 直线与圆

    题目描述 给出一个圆的圆心坐标与圆的半径,和一条直线上的两点坐标,求这条直线与圆有多少个交点. 输入 输入3个实数x,y,r值分别表示圆心坐标与圆的半径,输入4个实数x1,y1,x2,y2表示直线上的 ...

  6. Qt实现 动态化遍历二叉树(前中后层次遍历)

    binarytree.h 头文件 #ifndef LINKEDBINARYTREE_H #define LINKEDBINARYTREE_H #include<c++/algorithm> ...

  7. 我的代码- rf sampling

    # coding: utf-8 # In[6]: import pandas as pdimport numpy as npfrom sklearn import treefrom sklearn.s ...

  8. 【bug记录】OS Lab3 踩坑记

    OS Lab3 踩坑记 Lab3在之前Lab2的基础上,增加了进程建立.调度和中断异常处理.其中测试包括进程建立以及进程调度部分. 由于是第一次做bug记录,而且是调试完bug后再做的记录,所以导致记 ...

  9. C语言几个输入函数的区别(史上最详细)

    The difference of the string and the character(char): 字符串是一个带有""的字符序列如 "I fuck xuqian ...

  10. 深入理解CSS系列(二):为什么height:100%不生效?

    对于height属性,如果父元素height为auto,只要子元素在文档流中(即position不等于fixed或者absolute),其百分比值完全就被忽略了.这是什么意思呢?首先来看个例子,比如, ...