xoail
xoail

Reputation: 3064

List<IGroup> custom sort c#

I am trying to render a search results page in tabbed layout (with categories as tabs). How can I make sure the tab order to be Products, Articles and Videos? Right now it always prints alphabetical (coz of orderby method I suppose). I am quite new to creating custom sorts/comparison. Thank you!

protected void ResultsRedendering(List<Item> searchResult)
    {
        _searchResults = searchResult.GroupBy(i => i.TemplateName).OrderBy(p=>p.Key).ToList();
        _searchResults.RemoveAll(i => i.Key != "Product" && i.Key != "Article" && i.Key != "Video");
        rptResultTab.DataSource = _searchResults;
        rptResultTab.DataBind();
    }

This is how my item template renders each tab...

<ItemTemplate>
            <li><a href="#<%#((IGrouping<string , Item>)Container.DataItem).Key %>">
                <%#((IGrouping<string, Item>)Container.DataItem).Key)%></a></li>
</ItemTemplate>

Also, _searchResult is of type List<IGrouping<string,Item>>

Upvotes: 0

Views: 375

Answers (2)

ataddeini
ataddeini

Reputation: 4951

You can make separate lists of the categories to make them a little easier to work with:

var products = searchResult.Where(sr => sr.Key == "Product");
var articles = searchResult.Where(sr => sr.Key == "Article");
var videos = searchResult.Where(sr => sr.Key == "Video");

It's unclear to me how you have your DataBinding setup exactly, so this is all I can really recommend right now. If you add some more information I might be able to help further.

Upvotes: 1

Rob
Rob

Reputation: 1683

while maybe not 100% optimal this should work.

 protected void ResultsRedendering(List<Item> searchResult)
            {
                var _searchResults = searchResult
                 .OrderBy(o => o.Key == "Product" ? 1 : o.Key== "Article" ? 2 : 3});
                var rptResultTab.DataSource = _searchResults;
                rptResultTab.DataBind();
            }

Upvotes: 1

Related Questions