java ArrayList排序不區分大小寫

indexman發表於2017-11-24

最近在做程式碼勇士codewars的題目,順便記錄一下自己的解決方案。


1.排序類

1.1 不使用預定義比較器

package com.dylan.practice;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 不區分大小寫的升序排序List
 * @author xusucheng
 * @create 2017-11-23
 **/
public class Sorter {
    public static List<String> sort(List<String> textbooks) {
        Collections.sort(textbooks, new Comparator<String>(){
            public int compare(String s1, String s2) {
                return s1.toUpperCase().compareTo(s2.toUpperCase());
            }
        });
        return textbooks;
    }

    public static void main(String[] args) {
        List<String> unsort = new ArrayList<String>();
        for (String s : new String[]{"Algebra", "History", "Geometry", "english"}) {
            unsort.add(s);
        }

        System.out.println(sort(unsort));


    }

    //自定義比較類
    /*static class SortCaseNoSensitive implements Comparator<String>{
        public int compare(String s1, String s2) {
            return s1.toUpperCase().compareTo(s2.toUpperCase());
        }
    }*/
}

1.2 使用預定義比較器

import java.util.ArrayList;
import java.util.Collections;
class Sorter {
  public static ArrayList<String> sort(ArrayList<String> textbooks) {
    Collections.sort(textbooks, String.CASE_INSENSITIVE_ORDER);
    System.out.println(textbooks);
    return textbooks;
  }
}

2.測試類

/**
 * @author xusucheng
 * @create 2017-11-24
 **/
import com.dylan.practice.Sorter;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
import java.util.ArrayList;
import java.util.Collections;
import java.lang.Math;


public class SortingTest {

    @Test
    public void basicTest() {
        ArrayList<String> sorted = new ArrayList<String>();
        ArrayList<String> strs = new ArrayList<String>();
        for(String s : new String[] {"Algebra", "History", "Geometry", "English"})
            strs.add(s);
        for(String s : new String[] {"Algebra", "English", "Geometry", "History"})
            sorted.add(s);
        assertEquals(sorted, Sorter.sort(strs));
    }

    @Test
    public void capitalizationTest() {
        ArrayList<String> sorted = new ArrayList<String>();
        ArrayList<String> strs = new ArrayList<String>();
        for(String s : new String[] {"Algebra", "history", "Geometry", "english"})
            strs.add(s);
        for(String s : new String[] {"Algebra", "english", "Geometry", "history"})
            sorted.add(s);
        assertEquals(sorted, Sorter.sort(strs));
    }

    @Test
    public void symbolsTest() {
        ArrayList<String> sorted = new ArrayList<String>();
        ArrayList<String> strs = new ArrayList<String>();
        for(String s : new String[] {"Alg#bra", "$istory", "Geom^try", "**English"})
            strs.add(s);
        for(String s : new String[] {"$istory", "**English", "Alg#bra", "Geom^try"})
            sorted.add(s);
        assertEquals(sorted, Sorter.sort(strs));
    }


}



相關文章