라이브러리

[PHP] Collator::sort - 지정된 정렬기를 사용하여 배열 정렬




Collator::sort 소개


PHP 7.2 버전부터 Collator 클래스가 추가되었습니다. Collator 클래스는 문자열을 정렬하는 데 사용되는 인터페이스입니다. Collator::sort 메서드는 문자열을 정렬하는 데 사용됩니다.

Collator::sort 사용 예제


#hostingforum.kr
php

// Collator 인스턴스 생성

$collator = new Collator('ko_KR');



// 정렬할 문자열 배열

$array = ['apple', 'banana', 'orange', 'Apple', 'Banana'];



// Collator::sort 메서드 사용

usort($array, function($a, $b) use ($collator) {

    return $collator->compare($a, $b);

});



// 정렬된 결과 출력

print_r($array);



Collator::sort 옵션


Collator::sort 메서드는 여러 옵션을 지원합니다.

# 1. locale


Collator::sort 메서드는 locale 옵션을 사용하여 정렬 방식을 지정할 수 있습니다. 예를 들어, 'ko_KR'은 한국어를 사용하는 정렬 방식입니다.

#hostingforum.kr
php

$collator = new Collator('ko_KR');



# 2. case


Collator::sort 메서드는 case 옵션을 사용하여 대소문자 구별 여부를 지정할 수 있습니다. 예를 들어, 'upper'은 대문자로 정렬합니다.

#hostingforum.kr
php

$collator = new Collator('ko_KR', Collator::SORT_REGULAR, Collator::CASE_UPPER);



# 3. sort


Collator::sort 메서드는 sort 옵션을 사용하여 정렬 방식을 지정할 수 있습니다. 예를 들어, 'SORT_REGULAR'은 일반 정렬 방식입니다.

#hostingforum.kr
php

$collator = new Collator('ko_KR', Collator::SORT_REGULAR);



Collator::sort 메서드의 종류


Collator::sort 메서드는 여러 종류가 있습니다.

# 1. compare


compare 메서드는 두 문자열을 비교하여 정렬 순서를 반환합니다.

#hostingforum.kr
php

$collator = new Collator('ko_KR');

echo $collator->compare('apple', 'banana'); // -1

echo $collator->compare('banana', 'apple'); // 1

echo $collator->compare('apple', 'apple'); // 0



# 2. sort


sort 메서드는 문자열 배열을 정렬합니다.

#hostingforum.kr
php

$collator = new Collator('ko_KR');

$array = ['apple', 'banana', 'orange', 'Apple', 'Banana'];

$collator->sort($array);

print_r($array);



결론


Collator::sort 메서드는 문자열을 정렬하는 데 사용되는 인터페이스입니다. Collator::sort 메서드는 여러 옵션을 지원하며, compare, sort 메서드가 있습니다. Collator::sort 메서드는 PHP 7.2 버전부터 사용할 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 8,985건 / 586 페이지

검색

게시물 검색