I need to sort list of strings in the alphabetical order:
我需要按字母顺序对字符串列表进行排序:
List<String> list = new ArrayList();
list.add("development");
list.add("Development");
list.add("aa");
list.add("AA");
list.add("Aa");
A common way to do it is to use comparator:
一种常见的方法是使用比较器:
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
The problem of the CaseInsensitiveComparator that “AA” is equals to “aa”. Strings appear in the result according to the order of adding for the same values, and it is not correct:
CaseInsensitiveComparator的问题是“AA”等于“aa”。字符串会根据相同值的添加顺序显示在结果中,并且不正确:
"aa","AA","Aa","development","Development"
5 个解决方案
#1
63
If you don't want to add a dependency on Guava (per Michael's answer) then this comparator is equivalent:
如果您不想在Guava上添加依赖项(根据Michael的回答),则此比较器是等效的:
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:
该方法的最后4行可以更简洁地写成如下:
return (res != 0) ? res : str1.compareTo(str2);
#2
14
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
解决问题的简单方法是使用Guava的ComparisonChain 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"
#3
1
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);
}
})
);
#4
1
Simply use
java.util.Collections.sort(list)
without String.CASE_INSENSITIVE_ORDER comparator parameter.
没有String.CASE_INSENSITIVE_ORDER比较器参数。
#5
-1
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;
}
}
#1
63
If you don't want to add a dependency on Guava (per Michael's answer) then this comparator is equivalent:
如果您不想在Guava上添加依赖项(根据Michael的回答),则此比较器是等效的:
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:
该方法的最后4行可以更简洁地写成如下:
return (res != 0) ? res : str1.compareTo(str2);
#2
14
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
解决问题的简单方法是使用Guava的ComparisonChain 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"
#3
1
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);
}
})
);
#4
1
Simply use
java.util.Collections.sort(list)
without String.CASE_INSENSITIVE_ORDER comparator parameter.
没有String.CASE_INSENSITIVE_ORDER比较器参数。
#5
-1
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;
}
}