summaryrefslogtreecommitdiff
path: root/py/builtin.c
diff options
context:
space:
mode:
Diffstat (limited to 'py/builtin.c')
-rw-r--r--py/builtin.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/py/builtin.c b/py/builtin.c
index 078f4b49c..f73d41b0d 100644
--- a/py/builtin.c
+++ b/py/builtin.c
@@ -296,3 +296,29 @@ mp_obj_t mp_builtin_sum(int n_args, const mp_obj_t *args) {
}
return value;
}
+
+extern mp_obj_t list_sort(mp_obj_t args, mp_map_t *kwargs);
+static mp_obj_t mp_builtin_sorted(mp_obj_t args, mp_map_t *kwargs) {
+ mp_obj_t *args_items = NULL;
+ uint args_len = 0;
+
+ assert(MP_OBJ_IS_TYPE(args, &tuple_type));
+ mp_obj_tuple_get(args, &args_len, &args_items);
+ assert(args_len >= 1);
+ if (args_len > 1) {
+ nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError,
+ "must use keyword argument for key function"));
+ }
+ mp_obj_t iterable = rt_getiter(args_items[0]);
+ mp_obj_t self = rt_build_list(0, NULL);
+ mp_obj_t item;
+ while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) {
+ rt_list_append(self, item);
+ }
+
+ mp_obj_t new_args = rt_build_tuple(1, &self);
+ list_sort(new_args, kwargs);
+
+ return self;
+}
+MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_sorted_obj, mp_builtin_sorted);