diff --git a/src/types/list.rs b/src/types/list.rs index 2bf25d3e..15dfe9c9 100644 --- a/src/types/list.rs +++ b/src/types/list.rs @@ -111,6 +111,11 @@ impl PyList { index: 0, } } + + /// Sorts the list in-place. Equivalent to python `l.sort()` + pub fn sort(&self) -> PyResult<()> { + unsafe { err::error_on_minusone(self.py(), ffi::PyList_Sort(self.as_ptr())) } + } } /// Used by `PyList::iter()`. @@ -372,4 +377,21 @@ mod test { let v2 = list.as_ref().extract::>().unwrap(); assert_eq!(v, v2); } + + #[test] + fn test_sort() { + let gil = Python::acquire_gil(); + let py = gil.python(); + let v = vec![7, 3, 2, 5]; + let list = PyList::new(py, &v); + assert_eq!(7, list.get_item(0).extract::().unwrap()); + assert_eq!(3, list.get_item(1).extract::().unwrap()); + assert_eq!(2, list.get_item(2).extract::().unwrap()); + assert_eq!(5, list.get_item(3).extract::().unwrap()); + list.sort().unwrap(); + assert_eq!(2, list.get_item(0).extract::().unwrap()); + assert_eq!(3, list.get_item(1).extract::().unwrap()); + assert_eq!(5, list.get_item(2).extract::().unwrap()); + assert_eq!(7, list.get_item(3).extract::().unwrap()); + } }