Developer forum

Forum » Ecommerce - Standard features » Control sorting when passing the ProductIDs as filter parameters

Control sorting when passing the ProductIDs as filter parameters

Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi guys,

I have a situation that, I believe, applies to both Rapido and Swift, although in my situation it originated from Rapido.

In some situations, we send over a list of ProductIds to an endpoint/service that uses a Query to retrieve products.

The issue I have is the sorting of that list. Although the list is filtered correctly, I could not find a way to display the products in the order the ProductIDs were sent to the service.

Is there even such an option? If I would sort by Score, would that give me the order I need?

Thank you,
Adrian


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Sort by score will not give you the order you want. 

The resulting productlistviewmodel.products needs resorting - but will only work if you are not doing paging.

Otherwise you have to do a custom TopScoreDocCollector, see https://lucenenet.apache.org/docs/3.0.3/db/dfd/class_lucene_1_1_net_1_1_search_1_1_top_score_doc_collector.html

But I am not sure you can get Dynamicweb to detect it though....

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

I ended up reading the list of ProductIds from QueryString, transforming them into an array, and sorting the Products loop based on the order of the array.

It is not pretty but it seems to work with a reasonably sized list.

    string productIds = !string.IsNullOrEmpty(HttpContext.Current.Request["Combinations"]) ? HttpContext.Current.Request.QueryString.Get("Combinations") : "";
    string[] combinationsArray = productIds.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    List<ProductViewModel> productList = Model.Products.ToList();
    productList = productList.OrderBy(item =>
    {
        int index = Array.IndexOf(combinationsArray, item.Id);
        return index = index == -1 ? int.MaxValue : index;
    }
    ).ToList();

But I believe that you guys will have to figure out a standard way of handling this situation.

Thank you,

Adrian

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Glad you found a solution.

And that you want me to fix a difficult issue :-).

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

As usually :)

Adrian

 

You must be logged in to post in the forum