博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java 排序总结
阅读量:6310 次
发布时间:2019-06-22

本文共 4462 字,大约阅读时间需要 14 分钟。

hot3.png

Arrays.sort()

public static void sort(int[] a) Sorts the specified array into ascending numerical order. Implementation note: The sorting algorithm is a Dual-Pivot Quicksort by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm offers O(n log(n)) performance on many data sets that cause other quicksorts to degrade to quadratic performance, and is typically faster than traditional (one-pivot) Quicksort implementations.

Parameters: a - the array to be sorted

code

public class ArrayDemo {    public static void main(String[] args) {        // initializing unsorted int array        int iArr[] = {2, 1, 9, 6, 4};        // let us print all the elements available in list        for (int number : iArr) {            System.out.println("Number = " + number);        }        // sorting array        Arrays.sort(iArr);        // let us print all the elements available in list        System.out.println("The sorted int array is:");        for (int number : iArr) {            System.out.println("Number = " + number);        }    }}/*Output...Number = 2Number = 1Number = 9Number = 6Number = 4The sorted int array is:Number = 1Number = 2Number = 4Number = 6Number = 9*/

Collections.sort()

public static <T extends Comparable<? super T>> void sort(List<T> list) Sorts the specified list into ascending order, according to the natural ordering of its elements. All elements in the list must implement the Comparable interface. Furthermore, all elements in the list must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list). This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort. The specified list must be modifiable, but need not be resizable. Implementation Note: This implementation defers to the List.sort(Comparator) method using the specified list and a null comparator. Type Parameters: T - the class of the objects in the list Parameters: list - the list to be sorted.

code

实现comparable接口

/**  * 根据order对User排序 */  public class User implements Comparable
{ private String name; private Integer order; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getOrder() { return order; } public void setOrder(Integer order) { this.order = order; } public int compareTo(User arg0) { return this.getOrder().compareTo(arg0.getOrder()); } } 测试一下: public class Test{ public static void main(String[] args) { User user1 = new User(); user1.setName("a"); user1.setOrder(1); User user2 = new User(); user2.setName("b"); user2.setOrder(2); List
list = new ArrayList
(); //此处add user2再add user1 list.add(user2); list.add(user1); Collections.sort(list); for(User u : list){ System.out.println(u.getName()); } } }

Collections.sort重载方法来实现

public class User { //此处无需实现Comparable接口      private String name;       private Integer order;       public String getName() {           return name;       }       public void setName(String name) {           this.name = name;       }       public Integer getOrder() {           return order;       }       public void setOrder(Integer order) {           this.order = order;       }   }     主类中这样写即可(HastSet——>List——>sort进行排序):  public class Test {      public static void main(String[] args) {          User user1 = new User();          user1.setName("a");          user1.setPrice(11);          User user2 = new User();          user2.setName("b");          user2.setPrice(2);            Set
Hset = new HashSet
(); Hset.add(user2); Hset.add(user1); List
list = new ArrayList
(); list.addAll(Hset); Collections.sort(list,new Comparator
(){ public int compare(User arg0, User arg1) { return arg0.getPrice().compareTo(arg1.getPrice()); } }); for(User u : list){ System.out.println(u.getName()); } } /*输出结果如下:ab*/

References

转载于:https://my.oschina.net/Jerrymingzj/blog/803862

你可能感兴趣的文章
人脸识别 开放书籍 下载地址
查看>>
Notepad++配置Python开发环境
查看>>
用户组概念 和 挂载 概念
查看>>
如何快速获取ADO连接字符串
查看>>
AspNetPager控件的最基本用法
查看>>
sessionKey
查看>>
高性能Javascript--脚本的无阻塞加载策略
查看>>
Java 编程的动态性, 第4部分: 用 Javassist 进行类转换--转载
查看>>
完毕port(CompletionPort)具体解释 - 手把手教你玩转网络编程系列之三
查看>>
iOS8 Push Notifications
查看>>
各大名企笔试及面经大全(程序猿必读)
查看>>
Oracle 连接、会话数的查看,修改
查看>>
Python使用QRCode模块生成二维码
查看>>
英语学习的重要性
查看>>
Android中Handler引起的内存泄露
查看>>
原产地政策,jsonp跨域
查看>>
HDU 1143 Tri Tiling(递归)
查看>>
ffmpeg参数具体解释
查看>>
记一次公司仓库数据库服务器死锁过程
查看>>
Oracle 11g password过期被锁定报道 ORA-28000 the account is locked
查看>>