def test_sort_multiple_float_long(self): """ test_sort_multiple_float_long """ import math raw = [(1.0001, 2030L), (0.003, 10000L), (4.5, 2230L), (-0.223, 1222L), \ (1.22, -3120L, "aa"), (-0.002, -9000000000000000L)] pdata = self._pipeline.parallelize(raw) presult = pdata.sort_by( lambda record: [sort.ASC(record[0]), sort.DESC(record[1])]) def key_comparer(r1, r2): """ key_comparer """ if r1[0] < r2[0]: return -1 elif math.fabs(r1[0] - r2[0]) < 1e-7: if r1[1] < r2[1]: return 1 elif r1[1] > r2[1]: return -1 return 0 return 1 nresult = sorted(raw, cmp=key_comparer) self.assertEqual(nresult, self._pipeline.get(presult))
def test_sort_multiple_string(self): """ test_sort_multiple_string """ raw = [("a", "56"), ("e", "123"), ("c", "b231"), ("zzzz", "34555")] pdata = self._pipeline.parallelize(raw) presult = pdata.sort_by( lambda record: [sort.DESC(record[0]), sort.ASC(record[1])]) def key_comparer(r1, r2): """ key_comparer """ if r1[0] < r2[0]: return 1 elif r1[0] == r2[0]: if r1[1] < r2[1]: return 1 elif r1[1] > r2[1]: return -1 return 0 return -1 nresult = sorted(raw, cmp=key_comparer) self.assertEqual(nresult, self._pipeline.get(presult))
def test_sortby_multiple_key_long(self): """ test_sortby_multiple_key_long """ raw = [(15L, 2L), (2L, 4L), (2L, 40L), (10L, 20L), (15L, 10L), \ (-1073741824L, 14L), (-1073741824L, 12L), (1073741824L, 3L), (1073741824L, 1L)] pdata = self._pipeline.parallelize(raw) presult = pdata.sort_by( lambda record: [sort.ASC(record[0]), sort.DESC(record[1])]) def key_comparer(r1, r2): """ key_comparer """ if r1[0] == r2[0]: if r1[1] < r2[1]: return 1 elif r1[1] > r2[1]: return -1 else: return 0 elif r1[0] < r2[0]: return -1 else: return 1 nresult = sorted(raw, cmp=key_comparer) self.assertEqual(nresult, self._pipeline.get(presult))
def test_unicode_string_sort_total_chinese(self): """ test_unicode_string_sort_desc """ raw = [u'尽头', u'你好', u'深渊', u'', u'宝马bmw', u'hello世界'] nresult = sorted(raw) pdata = self._pipeline.parallelize(raw) presult = pdata.sort_by(lambda rec: sort.ASC(rec)) self.assertEqual(nresult, presult.get()) nresult_desc = sorted(raw, reverse=True) presult_desc = pdata.sort_by(lambda rec: sort.DESC(rec)) self.assertEqual(nresult_desc, presult_desc.get())
def test_sortby_single_key_int(self): """ test_sortby_single_key_int """ raw = [(12, -23), (15, -6), (10, 21), (8, 0), (44555, -2000000001), (23, 50), (15, 13)] pdata = self._pipeline.parallelize(raw) presult = pdata.sort_by(lambda record: sort.ASC(record[1])) def key_comparer(r1, r2): """ key_comparer """ return r1[1] - r2[1] nresult = sorted(raw, cmp=key_comparer) self.assertEqual(nresult, self._pipeline.get(presult))
def test_sortby_tuple_list_double_int(self): """ test_sortby_tuple_list_double_int """ raw = [(12, 23), (15, 6), (10, 21), (10, 11), (15, 2), (15, 9), (34, 1)] pdata = self._pipeline.parallelize(raw) presult = pdata.sort_by(lambda record: [sort.ASC(record[0]), sort.DESC(record[1])]) def key_comparer(r1, r2): """ key_comparer """ if (r1[0] != r2[0]): return r1[0] - r2[0] return r2[1] - r1[1] nresult = sorted(raw, cmp=key_comparer) self.assertEqual(self._pipeline.get(presult), nresult)
def test_sort_single_float(self): """ test_sort_single_float """ import math raw = [1.2, 2.3, 1.80001, 0.001, 0.0, 0.0003, \ -73786976294838206464.2, 1.8, 1.8000001, 2.334] pdata = self._pipeline.parallelize(raw) presult = pdata.sort_by(lambda record: sort.ASC(record)) def key_comparer(r1, r2): """ key_comparer """ if r1 < r2: return -1 elif math.fabs(r1 - r2) < 1e-7: return 0 return 1 nresult = sorted(raw, cmp=key_comparer) self.assertEqual(nresult, self._pipeline.get(presult))
def test_sortby_single_key_long(self): """ test_sortby_single_key_long """ raw = [(12L, -23L), (15L, -6L), (10L, 21L), (8L, 0L), (44555, -2147483644L),\ (34L, -222222223333333333L), (23, 50L), (15, 13L), (1L, 2222222222222222L), \ (1L, 2222222222222221L), (1L, 1073741823L), (2L, 1073741824L), \ (2L, 1073741825L), (2L, 1073741826L), (1L, -1073741823L), (2L, -1073741824L)] pdata = self._pipeline.parallelize(raw) presult = pdata.sort_by(lambda record: sort.ASC(record[1])) def key_comparer(r1, r2): """ key_comparer """ if r1[1] < r2[1]: return -1 elif r1[1] == r2[1]: return 0 else: return 1 nresult = sorted(raw, cmp=key_comparer) self.assertEqual(nresult, self._pipeline.get(presult))