함수 sort()

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 }}