Skip to content

Commit

Permalink
Merge pull request #357 from chr1sj0nes/master
Browse files Browse the repository at this point in the history
Add `PyList.sort()`.
  • Loading branch information
kngwyu committed Feb 17, 2019
2 parents 61194ab + 275e6cb commit 9ceb453
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()`.
Expand Down Expand Up @@ -372,4 +377,21 @@ mod test {
let v2 = list.as_ref().extract::<Vec<i32>>().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::<i32>().unwrap());
assert_eq!(3, list.get_item(1).extract::<i32>().unwrap());
assert_eq!(2, list.get_item(2).extract::<i32>().unwrap());
assert_eq!(5, list.get_item(3).extract::<i32>().unwrap());
list.sort().unwrap();
assert_eq!(2, list.get_item(0).extract::<i32>().unwrap());
assert_eq!(3, list.get_item(1).extract::<i32>().unwrap());
assert_eq!(5, list.get_item(2).extract::<i32>().unwrap());
assert_eq!(7, list.get_item(3).extract::<i32>().unwrap());
}
}

0 comments on commit 9ceb453

Please sign in to comment.