是否可以在忽略每个字符串中的前3个字符的同时对数组列表进行排序? [英] Is it possible to sort a an array list while ignoring the first 3 characters in each string?

查看:164
本文介绍了是否可以在忽略每个字符串中的前3个字符的同时对数组列表进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何按字母顺序对列表进行排序,通常这很容易,但我需要忽略列表中每个字符串的前5个字符。 (它们是数字IDS)

I am trying to figure out how to sort my list alphabetically, normally this would be really easy, but I need to ignore the first 5 characters of each string in my list. (they are numerical IDS)

        ArrayList<String> tempList = new ArrayList<String>();

        for(String s : AddressBook){
        tempList.add(s);

        Collections.sort(tempList );

            }
        System.out.println(tempList);


推荐答案

是的,这在Java 8中相当简单:

Yes this is fairly trivial in Java 8:

Collections.sort(list, Comparator.comparing(s -> s.subString(4));

这篇关于是否可以在忽略每个字符串中的前3个字符的同时对数组列表进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆