Simple way to sort strings in the (case sensitive) alphabetical order

前端 未结 5 2013
生来不讨喜
生来不讨喜 2020-11-27 19:56

I need to sort list of strings in the alphabetical order:

List list = new ArrayList();
list.add(\"development\");
list.add(\"Development\");
li         


        
相关标签:
5条回答
  • 2020-11-27 20:22
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Test3 {
        public static void main(String[] args) {
    
            String a;
            String b;
            try (Scanner scan = new Scanner(System.in)) {
                a = scan.next();
                b = scan.next();
            }
            boolean ret = isAnagram(a, b);
            System.out.println((ret) ? "Anagrams" : "Not Anagrams");
    
        }
    
        static boolean isAnagram(String a, String b) {
            int l1 = a.length();
            int l2 = b.length();
            boolean rat = false;
    
            if (l1 <= 50) {
                if (l1 == l2) {
    
                    char[] chars1 = a.toLowerCase().toCharArray();
                    char[] chars2 = b.toLowerCase().toCharArray();
                    Arrays.sort(chars1);
                    Arrays.sort(chars2);
                    String Ns1 = new String(chars1);
                    String Ns2 = new String(chars2);
                    if (Ns1.equals(Ns2)) {
                        rat = true;
                    }
                }
            }
            return rat;
        }
    }
    
    0 讨论(0)
  • 2020-11-27 20:26

    Simply use

    java.util.Collections.sort(list)
    

    without String.CASE_INSENSITIVE_ORDER comparator parameter.

    0 讨论(0)
  • 2020-11-27 20:45

    If you don't want to add a dependency on Guava (per Michael's answer) then this comparator is equivalent:

    private static Comparator<String> ALPHABETICAL_ORDER = new Comparator<String>() {
        public int compare(String str1, String str2) {
            int res = String.CASE_INSENSITIVE_ORDER.compare(str1, str2);
            if (res == 0) {
                res = str1.compareTo(str2);
            }
            return res;
        }
    };
    
    Collections.sort(list, ALPHABETICAL_ORDER);
    

    And I think it is just as easy to understand and code ...

    The last 4 lines of the method can written more concisely as follows:

            return (res != 0) ? res : str1.compareTo(str2);
    
    0 讨论(0)
  • 2020-11-27 20:45

    The simple way to solve the problem is to use ComparisonChain from Guava http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/ComparisonChain.html

    private static Comparator<String> stringAlphabeticalComparator = new Comparator<String>() {
            public int compare(String str1, String str2) {
                return ComparisonChain.start().
                                    compare(str1,str2, String.CASE_INSENSITIVE_ORDER).
                                    compare(str1,str2).
                                    result();
             }
     };
    Collections.sort(list, stringAlphabeticalComparator);
    

    The first comparator from the chain will sort strings according to the case insensitive order, and the second comparator will sort strings according to the case insensitive order. As excepted strings appear in the result according to the alphabetical order:

    "AA","Aa","aa","Development","development"
    
    0 讨论(0)
  • 2020-11-27 20:45

    I recently answered a similar question here. Applying the same approach to your problem would yield following solution:

    list.sort(
      p2Ord(stringOrd, stringOrd).comap(new F<String, P2<String, String>>() {
        public P2<String, String> f(String s) {
          return p(s.toLowerCase(), s);
        }
      })
    );
    
    0 讨论(0)
提交回复
热议问题