Skip to content

SortOption.limit combined with descending=True does not work for arg_sort #26833

@Max-Hoek

Description

@Max-Hoek

Checks

  • I have checked that this issue has not already been reported.
  • I have confirmed this bug exists on the latest version of Polars.

Reproducible example

use polars::prelude::*;

fn test_arg_sort_limit() {
    let arr: UInt32Chunked = ChunkedArray::from_vec("test".into(), vec![1, 2, 3]);

    let mut options = SortOptions::default()
        .with_order_descending(false);
    options.limit = Some(1);
    assert_eq!(arr.sort_with(options).to_vec()[0], Some(1)); // Passed
    assert_eq!(arr.arg_sort(options).to_vec()[0], Some(0)); // Passed

    let mut options = SortOptions::default()
        .with_order_reversed();
    options.limit = Some(1);
    assert_eq!(arr.sort_with(options).to_vec()[0], Some(3)); // Passed
    assert_eq!(arr.arg_sort(options).to_vec()[0], Some(2)); // Failed with Some(0)

    let mut options = SortOptions::default()
        .with_order_descending(true);
    options.limit = Some(1);
    assert_eq!(arr.sort_with(options).to_vec()[0], Some(3)); // Passed
    assert_eq!(arr.arg_sort(options).to_vec()[0], Some(2)); // Failed with Some(0)
}

Log output

Issue description

The SortOptions limit seems to only work correctly for arg_sort when sorting in ascending order.
The descending order option appears to be ignored or not working correctly when combined with limit

Expected behavior

The behavior of arg_sort is consistent with sort_with when provided the same SortOptions.

Installed versions

Details polars = { version = "0.53.0", default-features = false, features = ["dtype-full"] }

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds triageAwaiting prioritization by a maintainerrustRelated to Rust Polars

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions