"함수 rsort()"의 두 판 사이의 차이

 
(사용자 3명의 중간 판 14개는 보이지 않습니다)
4번째 줄: 4번째 줄:
* 내림차순 정렬
* 내림차순 정렬
* rsort([11,1,2,12]) → [12,11,2,1]
* rsort([11,1,2,12]) → [12,11,2,1]
* rsort(['Banana','Orange','Apple','Mango']) → ['Orange','Mango','Banana','Apple']


==Bash==
==Bash==
[[category: bash]]
[[category: bash]]
<source lang='bash'>
{{참고|Bash rsort}}
ARR=(11 1 2 12)
<syntaxhighlight lang='bash' run>
echo ${ARR[@]}
ARR=(11 1 12 2)
# 11 1 2 12
SORTED=`for VALUE in "${ARR[@]}"; do echo $VALUE; done | sort -rn`
RSORTED=`for VALUE in "${ARR[@]}"; do echo $VALUE; done | sort -rn`
echo ${SORTED[@]} # 12 11 2 1
echo ${RSORTED[@]}
</syntaxhighlight>
# 12 11 2 1
 
</source>
==C++==
[[분류: C++]]
{{참고|C++ rsort}}
<syntaxhighlight lang='cpp' run>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
int main() {
    vector<int> v = {11, 1, 12, 2};
    sort(v.begin(), v.end(), [](int a, int b) -> bool { return a > b; });
    for(int el: v) cout << el << ' '; // 12 11 2 1
}
</syntaxhighlight>
 
==Java==
[[분류: Java]]
{{참고|자바 reverseSort()}}
<syntaxhighlight lang='java' run>
import java.util.Arrays;
import java.util.Collections;
public class App {
    public static void main(String args[]) {
        Integer arr[] = {11, 1, 2, 12};
        Arrays.sort(arr, Collections.reverseOrder());
        System.out.println(Arrays.toString(arr)); // [12, 11, 2, 1]
    }
}
</syntaxhighlight>


==PHP==
==PHP==
[[category: PHP]]
[[category: PHP]]
<source lang='PHP'>
{{참고|PHP rsort()}}
$arr = array(11, 1, 2, 12);
<syntaxhighlight lang='PHP' run>
$arr = [11, 1, 12, 2];
rsort($arr);
rsort($arr);
print_r($arr);
foreach($arr as $i) echo "$i "; // 12 11 2 1
// Array
</syntaxhighlight>
// (
//    [0] => 12
//    [1] => 11
//    [2] => 2
//    [3] => 1
// )
</source>


==Python==
==Python==
[[category: Python]]
[[category: Python]]
<source lang='Python'>
{{참고|파이썬 rsort}}
<syntaxhighlight lang='Python' run>
lst = ['Banana', 'Orange', 'Apple', 'Mango']
lst = ['Banana', 'Orange', 'Apple', 'Mango']
lst.sort(reverse=True)
lst.sort(reverse=True)
print(lst)
print(lst) # ['Orange', 'Mango', 'Banana', 'Apple']
# ['Orange', 'Mango', 'Banana', 'Apple']
</syntaxhighlight>
</source>
<syntaxhighlight lang='Python' run>
<source lang='Python'>
lst = ['Banana', 'Orange', 'Apple', 'Mango']
lst = ['Banana', 'Orange', 'Apple', 'Mango']
lst.sort()
lst.sort()
lst.reverse()
lst.reverse()
print(lst)
print(lst) # ['Orange', 'Mango', 'Banana', 'Apple']
# ['Orange', 'Mango', 'Banana', 'Apple']
</syntaxhighlight>
</source>


==Perl==
==Perl==
[[category: Perl]]
[[category: Perl]]
<source lang='Perl'>
<syntaxhighlight lang='Perl'>
use Sort::Key qw(rsort);
my @lst = ('Banana', 'Orange', 'Apple', 'Mango');
my @lst = ('Banana', 'Orange', 'Apple', 'Mango');
print reverse @lst;
@lst = rsort @lst;
# MangoAppleOrangeBanana
print @lst;
</source>
# OrangeMangoBananaApple
</syntaxhighlight>


==같이 보기==
==같이 보기==

2023년 9월 17일 (일) 13:07 기준 최신판

1 개요[ | ]

함수 rsort()
  • 내림차순 정렬
  • rsort([11,1,2,12]) → [12,11,2,1]
  • rsort(['Banana','Orange','Apple','Mango']) → ['Orange','Mango','Banana','Apple']

2 Bash[ | ]

ARR=(11 1 12 2)
SORTED=`for VALUE in "${ARR[@]}"; do echo $VALUE; done | sort -rn`
echo ${SORTED[@]} # 12 11 2 1

3 C++[ | ]

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    vector<int> v = {11, 1, 12, 2};
    sort(v.begin(), v.end(), [](int a, int b) -> bool { return a > b; });
    for(int el: v) cout << el << ' '; // 12 11 2 1
}

4 Java[ | ]

import java.util.Arrays;
import java.util.Collections;
public class App {
    public static void main(String args[]) {
        Integer arr[] = {11, 1, 2, 12};
        Arrays.sort(arr, Collections.reverseOrder());
        System.out.println(Arrays.toString(arr)); // [12, 11, 2, 1]
    }
}

5 PHP[ | ]

$arr = [11, 1, 12, 2];
rsort($arr);
foreach($arr as $i) echo "$i "; // 12 11 2 1

6 Python[ | ]

lst = ['Banana', 'Orange', 'Apple', 'Mango']
lst.sort(reverse=True)
print(lst) # ['Orange', 'Mango', 'Banana', 'Apple']
lst = ['Banana', 'Orange', 'Apple', 'Mango']
lst.sort()
lst.reverse()
print(lst) # ['Orange', 'Mango', 'Banana', 'Apple']

7 Perl[ | ]

use Sort::Key qw(rsort);
my @lst = ('Banana', 'Orange', 'Apple', 'Mango');
@lst = rsort @lst;
print @lst;
# OrangeMangoBananaApple

8 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}