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

 
(같은 사용자의 중간 판 6개는 보이지 않습니다)
11번째 줄: 11번째 줄:
<syntaxhighlight lang='bash' run>
<syntaxhighlight lang='bash' run>
ARR=(11 1 12 2)
ARR=(11 1 12 2)
RSORTED=`for VALUE in "${ARR[@]}"; do echo $VALUE; done | sort -rn`
SORTED=`for VALUE in "${ARR[@]}"; do echo $VALUE; done | sort -rn`
echo ${RSORTED[@]}
echo ${SORTED[@]} # 12 11 2 1
# 12 11 2 1
</syntaxhighlight>
 
==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>
</syntaxhighlight>


19번째 줄: 34번째 줄:
[[분류: Java]]
[[분류: Java]]
{{참고|자바 reverseSort()}}
{{참고|자바 reverseSort()}}
<syntaxhighlight lang='java'>
<syntaxhighlight lang='java' run>
import java.util.Arrays;
import java.util.Arrays;
import java.util.Collections;
import java.util.Collections;
public class MyClass {
public class App {
     public static void main(String args[]) {
     public static void main(String args[]) {
         Integer arr[] = {11, 1, 2, 12};
         Integer arr[] = {11, 1, 2, 12};
         Arrays.sort(arr, Collections.reverseOrder());
         Arrays.sort(arr, Collections.reverseOrder());
         System.out.println(Arrays.toString(arr));
         System.out.println(Arrays.toString(arr)); // [12, 11, 2, 1]
        // [12, 11, 2, 1]
     }
     }
}
}
43번째 줄: 57번째 줄:
==Python==
==Python==
[[category: Python]]
[[category: Python]]
<syntaxhighlight 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>
</syntaxhighlight>
<syntaxhighlight lang='Python'>
<syntaxhighlight lang='Python' run>
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>
</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 }}