ComparisonChain-文件名排序
本文最后更新于 833 天前,其中的信息可能已经有所发展或是发生改变。

实现Comparable接口

@Getter
@Setter
@Builder
@ToString
public class MyFile implements Comparable<MyFile> {
    public Integer id;
    private String name;

    @Override
    public int compareTo(MyFile other) {
        //return this.id-other.id;
        return ComparisonChain.start()
                .compare(this.id,other.id)
                .compare(this.name,other.name)
                .result();
    }
}

遍历指定文件夹,并按照自定义规则排序

public class App {
    public static void main(String[] args) {
        File dir=new File("C:\\Users\\lenovo\\Desktop\\尚学堂Vue2.0");
        File[] files=dir.listFiles();
        List<MyFile> myFileList=new LinkedList<>();
        if(files!=null){
            for(File tmp:files){
                if (!tmp.isFile()){continue;}
                myFileList.add(MyFile.builder()
                        .id(Integer.valueOf(tmp.getName().substring(19,21).trim()))
                        .name(tmp.getName().substring(21).trim())
                        .build());               
            }
            myFileList.sort(MyFile::compareTo);
            Iterator iterator=myFileList.iterator();
            while (iterator.hasNext()){
                System.out.println(iterator.next());
            }
        }
    }
}
作者:Yuyy
博客:https://yuyy.info
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇