알파벳순으로 배열 목록 정렬 (대소 문자 구분)
names
사람들의 이름이 포함 된 문자열 배열 목록 이 있습니다. 알파벳순으로 배열 목록을 정렬하고 싶습니다.
ArrayList<String> names = new ArrayList<String>();
names.add("seetha");
names.add("sudhin");
names.add("Swetha");
names.add("Neethu");
names.add("ananya");
names.add("Athira");
names.add("bala");
names.add("Tony");
names.add("Karthika");
names.add("Nithin");
names.add("Vinod");
names.add("jeena");
Collections.sort(names);
for(int i=0; i<names.size(); i++)
System.out.println(names.get(i));
위의 방법으로 목록을 정렬하려고했습니다. 그러나 정렬 된 배열을 다음과 같이 표시합니다.
Athira
Karthika
..
..
ananya
bala
...
하지만 대소 문자를 구분하고 싶지 않습니다. 결과를 다음과 같이 원합니다.
ananya
Athira
bala
관습 Comparator
은 도움이되어야한다
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
});
또는 Java 8을 사용하는 경우 :
list.sort(String::compareToIgnoreCase);
가장 간단한 방법은 다음과 같습니다.
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
이 코드를 사용해보십시오
Collections.sort(yourarraylist, new SortBasedOnName());
import java.util.Comparator;
import com.RealHelp.objects.FBFriends_Obj;
import com.RealHelp.ui.importFBContacts;
public class SortBasedOnName implements Comparator
{
public int compare(Object o1, Object o2)
{
FBFriends_Obj dd1 = (FBFriends_Obj)o1;// where FBFriends_Obj is your object class
FBFriends_Obj dd2 = (FBFriends_Obj)o2;
return dd1.uname.compareToIgnoreCase(dd2.uname);//where uname is field name
}
}
위에서 언급 한 답변을 바탕으로 다음과 같이 사용자 정의 클래스 객체를 비교했습니다.
ArrayList<Item> itemList = new ArrayList<>();
...
Collections.sort(itemList, new Comparator<Item>() {
@Override
public int compare(Item item, Item t1) {
String s1 = item.getTitle();
String s2 = t1.getTitle();
return s1.compareToIgnoreCase(s2);
}
});
compareToIgnoreCase
compareTo가 아닌 을 사용할 사용자 지정 비교기를 사용해야 합니다.
Java 8 부터 다음을 사용할 수 있습니다 Stream
.
List<String> sorted = Arrays.asList(
names.stream().sorted(
(s1, s2) -> s1.compareToIgnoreCase(s2)
).toArray(String[]::new)
);
It gets a stream from that ArrayList
, then it sorts it (ignoring the case). After that, the stream is converted to an array which is converted to an ArrayList
.
If you print the result using:
System.out.println(sorted);
you get the following output:
[ananya, Athira, bala, jeena, Karthika, Neethu, Nithin, seetha, sudhin, Swetha, Tony, Vinod]
Unfortunately, all answers so far do not take into account that "a"
must not considered equal to "A"
when it comes to sorting.
String[] array = {"b", "A", "C", "B", "a"};
// Approach 1
Arrays.sort(array);
// array is [A, B, C, a, b]
// Approach 2
Arrays.sort(array, String.CASE_INSENSITIVE_ORDER);
// array is [A, a, b, B, C]
// Approach 3
Arrays.sort(array, java.text.Collator.getInstance());
// array is [a, A, b, B, C]
In approach 1 any lower case letters are considered greater than any upper case letters.
Approach 2 makes it worse, since CASE_INSENSITIVE_ORDER considers "a"
and "A"
equal (comparation result is 0
). This makes sorting non-deterministic.
Approach 3 (using a java.text.Collator) is IMHO the only way of doing it correctly, since it considers "a"
and "A"
not equal, but puts them in the correct order according to the current (or any other desired) Locale.
참고URL : https://stackoverflow.com/questions/5815423/sorting-arraylist-in-alphabetical-order-case-insensitive
'programing tip' 카테고리의 다른 글
일부 숫자에 천 단위 구분 기호로 쉼표가 포함 된 경우 데이터를 읽는 방법은 무엇입니까? (0) | 2020.07.25 |
---|---|
자바 스크립트 문자열에 변수를 어떻게 넣습니까? (0) | 2020.07.24 |
Windows Phone의 반응성 확장 프로그램 버그 (0) | 2020.07.24 |
22Mb의 총 메모리 사용량에도 불구하고 Haskell 스레드 힙 오버 플로우? (0) | 2020.07.24 |
Android 가상 장치를 다운로드 할 수있는 저장소가 있습니까? (0) | 2020.07.24 |