首页 > 其他分享 >Collections-sort

Collections-sort

时间:2022-11-04 19:00:44浏览次数:92  
标签:sort java s1 list add Collections public

import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

/*

* 1.Collections.sort(list);只能对List排序,注意:list中的
* 元素类型必须具备可比较性(继承Comparable)
2.Collections.sort(list,new StrLenComparator());//指定比较器升序
*/

public class CollectionsTest {

public static void main(String[] args) {

sortDemo();

}

public static void sortDemo(){

List<String> list=new ArrayList<String>();

list.add("aasd");

list.add("aaa");

list.add("aaa");

list.add("dd");

list.add("ss");

list.add("gg");

System.out.println(list.toString());

Collections.sort(list);//默认升序

System.out.println(list.toString());


Collections.sort(list,new StrLenComparator());//指定比较器升序

System.out.println(list.toString());

}


}
//**************************************************************
import java.util.Comparator;


public class StrLenComparator implements Comparator<String>{


@Override
public int compare(String s1, String s2) {
// TODO Auto-generated method stub
if(s1.length()>s2.length()){
return 1;
}
if(s1.length()<s2.length()){
return -1;
}

return s1.compareTo(s2);
}


}

标签:sort,java,s1,list,add,Collections,public
From: https://blog.51cto.com/u_10028442/5824710

相关文章

  • s-sort命令
    对文本操作进行排序,以行为单位,依次根据ascii值进行比较,默认的排序方式为升序sort[-bcfMnrtk][源文件][-o输出文件]补充说明:sort可针对文本文件的内容,以行为单位来排......
  • 排序之希尔排序(shell sort)
    前言本篇博客是在伍迷兄的博客基础上进行的,其​​博客地址​​点击就可以进去,里面好博客很多,我的排序算法都来自于此;一些数据结构方面的概念我就不多阐述了,伍迷兄的博客......
  • Delphi XE System.Generics.Collections[1] - 介绍
    DelphiXESystem.Generics.Collections[1]-介绍实现通用容器类以将数据项分组为数组、字典、列表、堆栈、队列等的单元。Package:rtl270.bpl ClassesTArrayCla......
  • Sort operation used more than the maximum 33554432 bytes of RAM
    在数据量超大的情形下,任何数据库系统在创建索引时都是一个耗时的大工程,下面这篇文章主要给大家介绍了关于MongoDB排序时内存大小限制与创建索引的注意事项的相关资料,需......
  • sort和sorted区别
    描述sort与sorted区别: sort是应用在list上的方法,属于列表的成员方法,sorted可以对所有可迭代的对象进行排序操作。list的sort方法返回的是对已经存在的列......
  • 4. Median of Two Sorted Arrays
    Giventwosortedarrays nums1 and nums2 ofsize m and n respectively,return themedian ofthetwosortedarrays.Theoverallruntimecomplexitysho......
  • C++ 实现argosort
    #include<bits/stdc++.h>usingnamespacestd;intmain(){intn=5,t;vector<int>a,b;for(inti=0;i<n;++i){scanf("%d",&t......
  • asp GridView AllowSorting 不生效
    最近在维护一个N年老项目,想着在Gridview设置 AllowSorting="true" 但排序还不管用。 <asp:GridViewID="GridView1"runat="server"AllowPaging="True"Auto......
  • Collections工具之sort
    作者:david_zhang@sh【转载时请以超链接形式标明文章】http://www.cnblogs.com/david-zhang-index/p/4246887.html当某个List<T>未实现Comparable<T>但又需要根据某个字段......
  • ysoserial commonscollections3 分析
    cc3利用链如下:TrAXFilter(Templatestemplates)TemplatesImpl->newTransformer()TemplatesImpl->getTransletInstance()_class[_transletInde......