Array Sort

From SwiftAPI

Jump to: navigation, search

Sort an existing array in-place. For creating a new array that contains the sorted values of an array see Array Sort Copy.

Contents

C

#include <stdlib.h>
 
int intcmp(const void *a, const void *b)
{
    // this works as long as the numbers are small enough
    // so that the subtraction doesn't overflow
    return *(const int *)a - *(const int *)b;
}
 
int numbers[] = { 2, 1, 2, 5, 0 };
qsort(numbers, 5, sizeof(int), intcmp);
// numbers = { 0, 1, 2, 2, 5 }

C++

#include <algorithm>
using namespace std;
 
int numbers[] = { 2, 1, 2, 5, 0 };
sort(numbers, numbers+5);
// numbers = { 0, 1, 2, 2, 5 }

C#

List<int> numbers = new List<int>() { 2, 1, 2, 5, 0 };
numbers.Sort();
// numbers == { 0, 1, 2, 2, 5 }

Java

Array

import java.util.Arrays;
 
int[] numbers = {2, 1, 2, 5, 0};
Arrays.sort(numbers);
// numbers = {0, 1, 2, 2, 5}

List

import java.util.*;
 
List<Integer> numbers = new ArrayList<Integer>(Arrays.asList(2, 1, 2, 5, 0));
Collections.sort(numbers);
// numbers = {0, 1, 2, 2, 5}

JavaScript

developer.mozilla.org

var numbers = [2, 1, 2, 5, 0];
numbers.sort(function (a, b) { return a - b; });
// numbers = [0, 1, 2, 2, 5]

Objective-C

Modifying a mutable array: developer.apple.com

- (void)sortUsingSelector:(SEL)comparator
NSMutableArray *numbers = [NSMutableArray arrayWithObjects:@2, @1, @2, @5, @0, nil];
[numbers sortUsingSelector:@selector(compare:)];
// numbers = [0, 1, 2, 2, 5]

or developer.apple.com

- (void)sortUsingComparator:(NSComparator)cmptr
NSMutableArray *numbers = [NSMutableArray arrayWithObjects:@2, @1, @2, @5, @0, nil];
[numbers sortUsingComparator:^NSComparisonResult(NSNumber *obj1, NSNumber *obj2) {
  return [obj1 compare:obj2];
}];
// numbers = [0, 1, 2, 2, 5]

Creating a new array without modifying: developer.apple.com

- (NSArray *)sortedArrayUsingSelector:(SEL)comparator
NSArray *numbers = [NSArray arrayWithObjects:@2, @1, @2, @5, @0, nil];
numbers = [numbers sortedArrayUsingSelector:@selector(compare:)];
// numbers = [0, 1, 2, 2, 5]

or developer.apple.com

- (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr
NSArray *numbers = [NSArray arrayWithObjects:@2, @1, @2, @5, @0, nil];
numbers = [numbers sortedArrayUsingComparator:^NSComparisonResult(NSNumber *obj1, NSNumber *obj2) {
  return [obj1 compare:obj2];
}];
// numbers = [0, 1, 2, 2, 5]

Perl

perldoc

sort BLOCK LIST
sort LIST

By default it sorts by string order. To sort by numerical order we need to provide a custom comparator.

@numbers = (2, 1, 2, 5, 0);
@numbers = sort {$a <=> $b} @numbers;
# @numbers == (0, 1, 2, 2, 5);

PHP

php.net

bool sort ( array &$array [, int $sort_flags = SORT_REGULAR ] )
$numbers = array(2, 1, 2, 5, 0);
sort($numbers);
// $numbers == array(0, 1, 2, 2, 5);

Python

numbers = [2, 1, 2, 5, 0]
numbers.sort()
# numbers == [0, 1, 2, 2, 5]

Ruby

Ruby-Doc.org

numbers = [2, 1, 2, 5, 0]
numbers.sort!
# numbers == [0, 1, 2, 2, 5]

Swift

var numbers = [2, 1, 2, 5, 0]
numbers.sort(<)
// numbers = [0, 1, 2, 2, 5]

or

var numbers = [2, 1, 2, 5, 0]
sort(&numbers)
// numbers = [0, 1, 2, 2, 5]
Personal tools