Java中的升序和降序
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中的升序和降序的更多相关文章
- Java排序之升序与降序
以前在学校学排序的时候,总是自己写排序的代码,真正到工作中,直接使用java提供的排序方法,但最近发现行业默认的和学习时有些不一样. 以前总是在进行排序时如果前边的数字和后边数字的差为负则交换两个数字 ...
- mysql中的升序和降序以及一个字段升序和一个字段降序
mySql中,升序为asc,降序为desc.例如: 升序:select * from 表名 order by 表中的字段 asc(mysql中默认是升序排列,可不写) 降序:select ...
- 【Java】数组升序和降序
int[] x={1,6,4,8,6,9,12,32,76,34,23}; 升序: Arrays.sort(x); 降序: resort(x); public int[] resort(int[] n ...
- 【转】java comparator 升序、降序、倒序从源码角度理解
原文链接:https://blog.csdn.net/u013066244/article/details/78997869 环境jdk:1.7+ 前言之前我写过关于comparator的理解,但是都 ...
- Java的数组和list升序,降序,逆序函数Collections.sort和Arrays.sort的使用
list升序,降序,逆序List<Integer>list =new ArrayList<Integer>();//如果list是 5 7 2 6 8 1 41.升序:Coll ...
- 通过orderby关键字,LINQ可以实现升序和降序排序。LINQ还支持次要排序。
通过orderby关键字,LINQ可以实现升序和降序排序.LINQ还支持次要排序. LINQ默认的排序是升序排序,如果你想使用降序排序,就要使用descending关键字. static void M ...
- DataGridView使用技巧十三:点击列头实现升序和降序排序
DataGridView 列有三种排序模式.每一列的排序模式是通过该列的 SortMode 属性指定的,该属性可以设置为以下的 DataGridViewColumnSortMode 枚举值之一. Da ...
- JDBC_10_使用Statement实现升序和降序
使用Statement数据库操作对象实现升序和降序 Statement可以使用在需要SQL语句拼接的情况下,因为在这样的情况下如果使用PreparedStatement就会给需要拼接的某个SQL关键字 ...
- 转<<C#集合Dictionary中按值的降序排列
转载地址:http://blog.sina.com.cn/s/blog_5c5bc9070100pped.html C#集合Dictionary中按值的降序排列 static void Main(st ...
随机推荐
- [LeetCode&Python] Problem 671. Second Minimum Node In a Binary Tree
Given a non-empty special binary tree consisting of nodes with the non-negative value, where each no ...
- 2.3 i++/i--与++i/--i的运算
一.i++/i--: i先参与运算,运算完成后自加/减1: public class Test{ public static void main(String[] args){ // [1] ; i+ ...
- html中头meta信息
一.页面关键字 网站关键字:用户通过搜索引擎能搜到该网站的词汇.最好控制在10个以内. 基本语法: <meta name="keywords" content="具 ...
- python之 类对象 类方法 实例对象 实例方法 静态方法
实例对象1. 创建的时间:使用 类名()的时候,就创建一个实例对象2. 实例属性:怎样添加 只要是一个变量能够指向这个实例对象,那么这个变量.xxxx = 111就是给其添加一个实例属性 特点: 跟着 ...
- 学习笔记CB010:递归神经网络、LSTM、自动抓取字幕
递归神经网络可存储记忆神经网络,LSTM是其中一种,在NLP领域应用效果不错. 递归神经网络(RNN),时间递归神经网络(recurrent neural network),结构递归神经网络(recu ...
- RedHat7系列(Centos/Debian) FireWall 防火墙 设置
RedHat 7 系列之后 系统把默认的iptables 换成了 Firewall 所以我们要适应 这个新的防火墙管理 -------------------服务相关----------------- ...
- 关于JS的一些案例,setInterval,动态图片
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...
- C# 如何保证对象线程内唯一:数据槽(CallContext)【转载】
如果说,一个对象保证全局唯一,大家肯定会想到一个经典的设计模式:单例模式,如果要使用的对象必须是线程内唯一的呢? 数据槽:CallContext,ok看下msdn对callcontent的解释. Ca ...
- Django学习笔记之上下文处理器和中间件
上下文处理器 上下文处理器是可以返回一些数据,在全局模板中都可以使用.比如登录后的用户信息,在很多页面中都需要使用,那么我们可以放在上下文处理器中,就没有必要在每个视图函数中都返回这个对象. 在set ...
- Django学习笔记之视图高级-HTTP请求与响应
Django限制请求method 常用的请求method GET请求 GET请求一般用来向服务器索取数据,但不会向服务器提交数据,不会对服务器的状态进行更改.比如向服务器获取某篇文章的详情. POST ...