栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在不区分大小写的情况下按字母顺序排序?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在不区分大小写的情况下按字母顺序排序?

这是最佳方法的简单Java示例:

import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;public class Sorter {    String fruits[] = new String[7];    List<String> lst;    Sorter() {        lst = new ArrayList<String>();        // initialise UNSORTED array        fruits[0] = "Melon"; fruits[1] = "apricot"; fruits[2] = "peach";        fruits[3] = "mango"; fruits[4] = "Apple";   fruits[5] = "pineapple";        fruits[6] = "banana";    }    public static void main(String[] args) {        Sorter srt = new Sorter();        srt.anyOldUnstaticMethod();    }    public void anyOldUnstaticMethod() {        Collections.addAll(lst, fruits);        System.out.println("Initial List");        for (String s : lst) System.out.println(s);        Collections.sort(lst);        System.out.println("nSorted List");        for (String s : lst) System.out.println(s);        Collections.sort(lst, new SortIgnoreCase());        System.out.println("nSorted Ignoring Case List");        for (String s : lst) System.out.println(s);    }    public class SortIgnoreCase implements Comparator<Object> {        public int compare(Object o1, Object o2) { String s1 = (String) o1; String s2 = (String) o2; return s1.toLowerCase().compareTo(s2.toLowerCase());        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/485721.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号