Skip to content

Pagination by category assigns post to wrong category #932

Open
@deep110

Description

@deep110

When creating the list of categories and assigning posts to it before pagination, wrong category is chosen because of sort.

Code can be found in categories.rs in parse_categories_list
Snippet:

let mut cur_cat = if let Ok(idx) = parent.sub_cats.binary_search_by(|c| {
            compare_category_path(
                c.cat_path.iter().map(|v| v.as_view()),
                cat_full_path.iter().copied(),
            )
        }) {
            &mut parent.sub_cats[idx]
        } else {
            let last_idx = parent.sub_cats.len();
            parent
                .sub_cats
                .push(Category::with_path(cat_full_path.into_iter()));
            // need to sort for binary_search_by
            parent.sub_cats.sort_by(|c1, c2| {
                compare_category_path(
                    c1.cat_path.iter().map(|v| v.as_view()),
                    c2.cat_path.iter().map(|v| v.as_view()),
                )
            });
            &mut parent.sub_cats[last_idx]
        };

Here if order is changed because of sorting then last_idx will return the wrong category.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugNot as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions