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

 
(사용자 2명의 중간 판 9개는 보이지 않습니다)
5번째 줄: 5번째 줄:
==Bash==
==Bash==
[[category: bash]]
[[category: bash]]
<source lang='bash'>
{{참고|Bash sort}}
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 -n`
SORTED=`for VALUE in "${ARR[@]}"; do echo $VALUE; done | sort -n`
echo ${SORTED[@]}
echo ${SORTED[@]} # 1 2 11 12
# 1 2 11 12
</syntaxhighlight>
</source>


==C++==
==C++==
[[분류: C++]]
[[분류: C++]]
{{참고|C++ 배열 정렬}}
{{참고|C++ sort()}}
<source lang='cpp'>
<syntaxhighlight lang='cpp' run>
#include <iostream>
#include <iostream>
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
using namespace std;
int main() {
int main() {
     int nums[] = {3,4,2,1};
     int nums[] = {11, 1, 12, 2};
     int len = sizeof(nums)/sizeof(int);
     int len = sizeof(nums)/sizeof(int);
     sort(nums, nums+len);
     sort(nums, nums+len);
     for(int i=0; i<len; i++) cout << nums[i] << " ";
     for(int i=0; i<len; i++) cout << nums[i] << ' '; // 1 2 11 12
     // 1 2 3 4
}
</syntaxhighlight>
<syntaxhighlight lang='cpp' run>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
     vector<int> nums = {11, 1, 12, 2};
    sort(nums.begin(), nums.end());
    for(auto& n: nums) cout << n << ' '; // 1 2 11 12
}
}
</source>
</syntaxhighlight>


==CMD==
==CMD==
34번째 줄: 42번째 줄:
[[분류: cmd]]
[[분류: cmd]]
* Sort alphabetically
* Sort alphabetically
<source lang='dos'>
<syntaxhighlight lang='dos'>
(
(
echo 3
echo 3
45번째 줄: 53번째 줄:
REM 3
REM 3
REM 4
REM 4
</source>
</syntaxhighlight>
<source lang='dos'>
<syntaxhighlight lang='dos'>
(
(
echo 11
echo 11
57번째 줄: 65번째 줄:
REM 12
REM 12
REM 2
REM 2
</source>
</syntaxhighlight>
<source lang='dos'>
<syntaxhighlight lang='dos'>
(
(
echo Carol
echo Carol
69번째 줄: 77번째 줄:
REM Carol
REM Carol
REM Dave
REM Dave
</source>
</syntaxhighlight>
* Sort numerically (sortn)
* Sort numerically (sortn)
https://www.dostips.com/?t=Batch.SortTextWithNumbers
https://www.dostips.com/?t=Batch.SortTextWithNumbers
<source lang='dos'>
<syntaxhighlight lang='dos'>
(
(
echo 11
echo 11
83번째 줄: 91번째 줄:
REM 11
REM 11
REM 12
REM 12
</source>
</syntaxhighlight>


==Java==
==Java==
[[분류: Java]]
[[분류: Java]]
{{참고|자바 sort()}}
{{참고|자바 sort()}}
<source lang='java'>
<syntaxhighlight lang='java'>
import java.util.Arrays;
import java.util.Arrays;
public class MyClass {
public class MyClass {
98번째 줄: 106번째 줄:
     }
     }
}
}
</source>
</syntaxhighlight>


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


==Python==
==Python==
[[category: Python]]
[[category: Python]]
<source lang='Python'>
<syntaxhighlight lang='Python'>
lst = [11, 1, 2, 12]
lst = [11, 1, 2, 12]
lst = sorted(lst)
lst = sorted(lst)
print lst
print lst
// [1, 2, 11, 12]
// [1, 2, 11, 12]
</source>
</syntaxhighlight>


==Perl==
==Perl==
[[category: Perl]]
[[category: Perl]]
<source lang='perl'>
<syntaxhighlight lang='perl'>
# Sort numerically
# Sort numerically
my @arr = (11, 1, 2, 12);
my @arr = (11, 1, 2, 12);
135번째 줄: 136번째 줄:
}
}
# 1 2 11 12
# 1 2 11 12
</source>
</syntaxhighlight>


<source lang='perl'>
<syntaxhighlight lang='perl'>
# Sort alphabetically
# Sort alphabetically
my @arr = ('Mango', 'Apple');
my @arr = ('Mango', 'Apple');
145번째 줄: 146번째 줄:
}
}
# Apple Mango
# Apple Mango
</source>
</syntaxhighlight>
 
==R==
[[분류: R]]
{{참고|R sort()}}
<syntaxhighlight lang='r'>
v <- c(11, 1, 2, 12)
sort( v )
## [1]  1  2 11 12
</syntaxhighlight>


==Ruby==
==Ruby==
[[category: Ruby]]
[[category: Ruby]]
<source lang='Ruby'>
<syntaxhighlight lang='Ruby'>
arr = [11, 1, 2, 12]
arr = [11, 1, 2, 12]
arr2 = arr.sort
arr2 = arr.sort
print arr2
print arr2
# [1, 2, 11, 12]
# [1, 2, 11, 12]
</source>
</syntaxhighlight>
<source lang='Ruby'>
<syntaxhighlight lang='Ruby'>
arr = [11, 1, 2, 12]
arr = [11, 1, 2, 12]
arr.sort!
arr.sort!
print arr
print arr
# [1, 2, 11, 12]
# [1, 2, 11, 12]
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==
* [[함수 sorted()]]
* [[함수 rsort()]]
* [[함수 rsort()]]
* [[함수 print_r()]]
* [[함수 print_r()]]

2023년 9월 17일 (일) 12:53 기준 최신판

sort
sorted

1 Bash[ | ]

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

2 C++[ | ]

#include <iostream>
#include <algorithm>
using namespace std;
int main() {
    int nums[] = {11, 1, 12, 2};
    int len = sizeof(nums)/sizeof(int);
    sort(nums, nums+len);
    for(int i=0; i<len; i++) cout << nums[i] << ' '; // 1 2 11 12
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
    vector<int> nums = {11, 1, 12, 2};
    sort(nums.begin(), nums.end());
    for(auto& n: nums) cout << n << ' '; // 1 2 11 12
}

3 CMD[ | ]

  • Sort alphabetically
(
echo 3
echo 4
echo 2
echo 1
) | sort
REM 1
REM 2
REM 3
REM 4
(
echo 11
echo 1
echo 2
echo 12
) | sort
REM 1
REM 11
REM 12
REM 2
(
echo Carol
echo Alice
echo Bob
echo Dave
) | sort
REM Alice
REM Bob
REM Carol
REM Dave
  • Sort numerically (sortn)

https://www.dostips.com/?t=Batch.SortTextWithNumbers

(
echo 11
echo 1
echo 2
echo 12
) | sortn
REM 1
REM 2
REM 11
REM 12

4 Java[ | ]

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

5 PHP[ | ]

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

6 Python[ | ]

lst = [11, 1, 2, 12]
lst = sorted(lst)
print lst
// [1, 2, 11, 12]

7 Perl[ | ]

# Sort numerically
my @arr = (11, 1, 2, 12);
@arr = sort { $a <=> $b } @arr;
foreach my $ls (@arr) {
	print $ls . " ";
}
# 1 2 11 12
# Sort alphabetically
my @arr = ('Mango', 'Apple');
@arr = sort @arr;
foreach my $ls (@arr) {
	print $ls . " ";
}
# Apple Mango

8 R[ | ]

v <- c(11, 1, 2, 12)
sort( v )
## [1]  1  2 11 12

9 Ruby[ | ]

arr = [11, 1, 2, 12]
arr2 = arr.sort
print arr2
# [1, 2, 11, 12]
arr = [11, 1, 2, 12]
arr.sort!
print arr
# [1, 2, 11, 12]

10 같이 보기[ | ]

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