Main Content

sortBy

Class: matlab.compiler.mlspark.RDD
Package: matlab.compiler.mlspark

Sort an RDD by a given function

Syntax

result = sortBy(obj,func,numPartitions)

Description

result = sortBy(obj,func,numPartitions) sorts obj using a given func. numPartitions specifies the number of partitions to create in the resulting RDD.

Input Arguments

expand all

An input RDD specified as a RDD object.

Function that computes the sort key for each element in the input RDD, specified as a function handle.

Data Types: function_handle

A scalar value specifying the number of partitions to create, returned as a RDD object.

Data Types: double

Output Arguments

expand all

An output pipelined RDD.

Examples

expand all

%% Connect to Spark
sparkProp = containers.Map({'spark.executor.cores'}, {'1'});
conf = matlab.compiler.mlspark.SparkConf('AppName','myApp', ...
                        'Master','local[1]','SparkProperties',sparkProp);
sc = matlab.compiler.mlspark.SparkContext(conf);

%% sortBy
x = sc.parallelize({ {'a', 50}, {'b', 20}, {'f', 40}, {'d', 30}, {'2',5} });
% sort by 2nd element in each key-value pair
z = x.sortBy(@(x)(x{2}));
viewRes = z.collect() % {{'2',5},{'b', 20},{'d', 30},{'f', 40}, {'a', 50}}
Introduced in R2016b