python/pyspark/rdd.py
def takeOrdered(self, num, key=None):
        """
        Get the N elements from a RDD ordered in ascending order or as specified
        by the optional key function. 

        >>> sc.parallelize([10, 1, 2, 9, 3, 4, 5, 6, 7]).takeOrdered(6)
        [1, 2, 3, 4, 5, 6]
        >>> sc.parallelize([10, 1, 2, 9, 3, 4, 5, 6, 7], 2).takeOrdered(6, key=lambda x: -x)
        [10, 9, 7, 6, 5, 4]
        """

        def topNKeyedElems(iterator, key_=None):
            q = MaxHeapQ(num)
            for k in iterator:
                if key_ != None:
                    k = (key_(k), k)
                q.insert(k)
            yield q.getElements()

        def unKey(x, key_=None):
            if key_ != None:
                x = [i[1] for i in x]
            return x
        
        def merge(a, b):
            return next(topNKeyedElems(a + b))
        result = self.mapPartitions(lambda i: topNKeyedElems(i, key)).reduce(merge)
        return sorted(unKey(result, key), key=key)

        Get the N elements from a RDD ordered in ascending order or as specified
        by the optional key function. 

        >>> sc.parallelize([10, 1, 2, 9, 3, 4, 5, 6, 7]).takeOrdered(6)
        [1, 2, 3, 4, 5, 6]
        >>> sc.parallelize([10, 1, 2, 9, 3, 4, 5, 6, 7], 2).takeOrdered(6, key=lambda x: -x)
        [10, 9, 7, 6, 5, 4]
        
Get the N elements from a RDD ordered in ascending order or as specified
by the optional key function.


Get the N elements from a RDD ordered in ascending order or as specified by the optional key function .
def takeOrdered(self, num, key=None):
        def topNKeyedElems(iterator, key_=None):
            q = MaxHeapQ(num)
            for k in iterator:
                if key_ != None:
                    k = (key_(k), k)
                q.insert(k)
            yield q.getElements()
        def unKey(x, key_=None):
            if key_ != None:
                x = [i[1] for i in x]
            return x
        def merge(a, b):
            return next(topNKeyedElems(a + b))
        result = self.mapPartitions(lambda i: topNKeyedElems(i, key)).reduce(merge)
        return sorted(unKey(result, key), key=key)
def takeOrdered ( self , num , key = None ) : def topNKeyedElems ( iterator , key_ = None ) : q = MaxHeapQ ( num ) for k in iterator : if key_ != None : k = ( key_ ( k ) , k ) q . insert ( k ) yield q . getElements ( ) def unKey ( x , key_ = None ) : if key_ != None : x = [ i [ 1 ] for i in x ] return x def merge ( a , b ) : return next ( topNKeyedElems ( a + b ) ) result = self . mapPartitions ( lambda i : topNKeyedElems ( i , key ) ) . reduce ( merge ) return sorted ( unKey ( result , key ) , key = key )
Get the N elements from a RDD ordered in ascending order or as specified
by the optional key function.
Get the N elements from a RDD ordered in ascending order or as specified by the optional key function .
"""
        Get the N elements from a RDD ordered in ascending order or as specified
        by the optional key function. 

        >>> sc.parallelize([10, 1, 2, 9, 3, 4, 5, 6, 7]).takeOrdered(6)
        [1, 2, 3, 4, 5, 6]
        >>> sc.parallelize([10, 1, 2, 9, 3, 4, 5, 6, 7], 2).takeOrdered(6, key=lambda x: -x)
        [10, 9, 7, 6, 5, 4]
        """
self
num
key