라이브러리
[PHP] DsVector::sort - 벡터를 그 자리에서 정렬합니다.
PHP 에서 DsVector::sort는 DsVector 클래스의 메소드로, 내부적으로 사용하는 데이터를 오름차순으로 정렬합니다.
DsVector::sort 사용법
DsVector::sort 메소드는 다음과 같은 형식으로 사용할 수 있습니다.
#hostingforum.kr
php
$vector->sort($mode = SORT_REGULAR);
- `$vector`: 정렬을 수행할 DsVector 객체
- `$mode`: 정렬 모드 (SORT_REGULAR, SORT_NUMERIC, SORT_STRING)
정렬 모드
- SORT_REGULAR: 기본 정렬 모드 (문자열, 숫자, 배열 등)
- SORT_NUMERIC: 숫자만 정렬
- SORT_STRING: 문자열만 정렬
예제
#hostingforum.kr
php
// DsVector 객체 생성
$vector = new SplDoublyLinkedList();
// 데이터 추가
$vector->push(5);
$vector->push(2);
$vector->push(8);
$vector->push(1);
$vector->push(9);
// 정렬 수행
$vector->sort();
// 정렬된 데이터 출력
while ($element = $vector->current()) {
echo "$element
";
$vector->next();
}
실행 결과:
#hostingforum.kr
1
2
5
8
9
정렬 모드 사용 예제
#hostingforum.kr
php
// DsVector 객체 생성
$vector = new SplDoublyLinkedList();
// 데이터 추가
$vector->push('apple');
$vector->push('banana');
$vector->push('orange');
$vector->push('grape');
// SORT_STRING 모드 사용
$vector->sort(SORT_STRING);
// 정렬된 데이터 출력
while ($element = $vector->current()) {
echo "$element
";
$vector->next();
}
실행 결과:
#hostingforum.kr
apple
banana
grape
orange
정렬 모드 사용 예제 (숫자)
#hostingforum.kr
php
// DsVector 객체 생성
$vector = new SplDoublyLinkedList();
// 데이터 추가
$vector->push(5);
$vector->push(2);
$vector->push(8);
$vector->push(1);
$vector->push(9);
// SORT_NUMERIC 모드 사용
$vector->sort(SORT_NUMERIC);
// 정렬된 데이터 출력
while ($element = $vector->current()) {
echo "$element
";
$vector->next();
}
실행 결과:
#hostingforum.kr
1
2
5
8
9
정렬 모드 사용 예제 (배열)
#hostingforum.kr
php
// DsVector 객체 생성
$vector = new SplDoublyLinkedList();
// 데이터 추가
$vector->push([1, 2]);
$vector->push([3, 4]);
$vector->push([5, 6]);
$vector->push([7, 8]);
$vector->push([9, 10]);
// SORT_REGULAR 모드 사용
$vector->sort();
// 정렬된 데이터 출력
while ($element = $vector->current()) {
echo implode(', ', $element) . "
";
$vector->next();
}
실행 결과:
#hostingforum.kr
1, 2
3, 4
5, 6
7, 8
9, 10
이러한 예제를 통해 DsVector::sort 메소드의 사용법과 정렬 모드의 차이를 이해할 수 있습니다.
-
- 나우호스팅 @pcs8404
-
호스팅포럼 화이팅!
댓글목록
등록된 댓글이 없습니다.