Java中Collections.sort()排序详解

默认是按照升序排列的。

要使用Collections.sort(),看java源码中的内容:


首先我们再使用的时候都是这样的:
Collections.sort(List list , Comparator<? super T> c)
首先我们看一下官方文档的描述

Sorts the specified list according to the order induced by the specified comparator. All elements in the list must be mutually comparable using the specified comparator (that is, c.compare(e1, 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.

这里意识流翻译一下:
  你要排序就用我的sort()方法就行,但是要保证list中的内容是可比较的或者有比较器的存在。什么叫可比较的呢?那就是实现Comparable接口啦~于是引出第一种排序方式------【实现Comparable接口,不然怎么可能会排好序呢】

【作者:leaf_eater 链接:https://www.jianshu.com/p/ec0db5191758 來源:简书】

List<Integer> 也可以哦,更别说 ArrayList<String> 了,因为java内包括Integer 的封装类在内都实现了这个接口。

Collections.sort(list, new Comparator<PCB>() {
    @Override    
      public int compare(PCB o1, PCB o2) {        
      return o2.getID() - o1.getID();  //重写comparator()本来是 o1<o2:-1   o1==2:0   o1>o2:1
    }});

举个栗子:

import java.util.*;
public class CollectionsDemo {
     public static void main(String args[]) {
      // create an array of string objs
      String init[] = { "One", "Two", "Three", "One", "Two", "Three" }; 
      // create one list
      List list = new ArrayList(Arrays.asList(init));
      System.out.println("List value before: "+list);
      // sort the list
      Collections.sort(list); //按照升序排列
      System.out.println("List value after sort: "+list);
   }
}

还有 全排列 字符串的题目也可以使用这个:

import java.util.ArrayList;
import java.util.*;
public class Solution {
    public ArrayList<String> Permutation(String str) {
        ArrayList<String> res  = new ArrayList<String>() ; //数据结构主要
        HashSet<String> set = new HashSet<String>(); //HashSet 类部分放在下一篇讲。
        if(str == null || str.length()==0){
            return res;
        }
        permutation(set,str.toCharArray(),0);
        res.addAll(set);
        Collections.sort(res);//牛客网上不用这一步 会只有40%通过率,eg:"aab":aab aba baa;如果不用出现 aba aab baa与结果不匹配所以加一个sort
        return res;
    }
    public void permutation(HashSet<String> set,char[] s,int k){//k:标记交换进行到的位置
        if(k == s.length-1){////标记进行到最后一位 输出排序数组
            set.add(new String(s));
        }
        for(int i = k;i<s.length;i++){
            if(i!=k && s[i]==s[k]){
                continue;//避免相同字符之间的交换,不进行下面交换,跳过本次循环
            }
            swap(s,i,k);//交换数组的i和k位
            permutation(set,s,k+1);//回溯-递归步骤
            swap(s,i,k);//及时换回来,方便本字符与其他字符交换 ,注意这里是有递归的,跟的是上上步的swap();
        }
    }
    public static void swap(char[]s,int i,int j){
        char temp = s[i];
        s[i] = s[j];
        s[j] = temp;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值