Comparator
및 오렌지 과일을 처음 사용하는 방법을 알고 싶었던 post을 확인했습니다. 포스트 toString 메소드 그렇게 없어진에서 나는 포스트에 주어진 대답했다Arrays.parallelSort vs Collections.sort
@Override
public String toString(){
return fruitName +" " + fruitDesc;
}
내 코드에 추가
사용 Collection.sort
Collections.sort(fruits, new Comparator<Fruit>() {
@Override
public int compare(Fruit o1, Fruit o2) {
if (o1.getFruitName() != null && o1.getFruitName().equalsIgnoreCase("orange")){
return -1;
}
if (o2.getFruitName() != null && o2.getFruitName().equalsIgnoreCase("orange")){
return 1;
}
return o1.getFruitName().compareTo(o2.getFruitName());
}
});
출력 :
Orange Orange description
Apple Apple description
Banana Banana description
Pineapple Pineapple description
Pineapple Pineapple description
Apple Apple description
Orange Orange description
Banana Banana description
,321 : 나는
이 Arrays.parallelSort
코드
Fruit[] arrayFruits = fruits.stream().toArray(Fruit[]::new);
Arrays.parallelSort(arrayFruits, (Fruit o1, Fruit o2) -> {
if (o1.getFruitName() != null && o1.getFruitName().equalsIgnoreCase("orange")){
return -1;
}
if (o2.getFruitName() != null && o2.getFruitName().equalsIgnoreCase("orange")){
return 1;
}
return o1.getFruitName().compareTo(o2.getFruitName());
});
출력 사용에 대한 좋은 물건을 말씀 드리 지요하지 Arrays.parallelSort
하는 이유는 6,510,403,210을 생각했다 0
저에게 정렬은 왜 다른 대답 양식 다른 방법을 정렬입니까?
이러한 [link1] (http://stackoverflow.com/questions/17328077/difference-between-arrays-sort-and-arrays-parallelsort) 및 [link2] (http://java.dzone.com/ articles/arrayssort-versus) 도움이됩니다. –
@ ankur-singhal 감사합니다. 그러나 이미 확인했습니다. –
@KickButtowski : 프로그램을 올바르게 실행 했습니까? 값을 입력하는 순서와 관계없이 '오렌지색'이 가장 먼저 배치됩니다. –