Kentico 12 MVC: How to order pages by the same order they where entered

Erick Valdivieso asked on May 10, 2019 17:50

Hi, I created a Widget with one textarea field where the admin can enter NodeGUID(s) per line of the products they want to display in the feed.

I am having trouble listing them in the same order they were entered, for example if the following was entered:
1,2,3,4,5

Then it would output in this order:
1,2,4,3,5

Here is the method I am using in my controller:

public ActionResult GetAllProducts(string ListGuid)
   {
       var listItems = ProductProvider.GetProducts()
                    .Columns("DocumentName", "NodeGUID", "ProductName")
                    .WhereIn("NodeGuid", ListGuid.Split('\n').Select(p => p.Trim()).ToList())
                    .OrderByDefault();

                var model = listItems.Select(item => new ProductViewModel()
                {
                    DocumentName = item.DocumentName,
                    ProductName = item.ProductName
                });

                return PartialView("DisplayTemplates/_ProductsAll", model);
            }

Then for example, the widget textare field would have the the following value entered:

7296ae5a-4cb1-4bd1-8850-33ed48a7e8a2
fb9e363a-273b-4269-911a-0dbbdb5cd67e
79cd4c15-17ac-4772-9189-9c6f7464fc6a
1fff86a0-1427-4011-a64a-424b1390f887
73e5c127-753a-4568-aa14-9b0e35bf81a1
9f572220-0a65-4f04-8932-291c17d29e82

Any suggestions would be appreciated, thanks.

Correct Answer

Peter Mogilnitski answered on May 10, 2019 21:10

you want to order by the items in the another list:

  ProductProvider.GetProducts()
            .Columns("DocumentName", "NodeGUID", "ProductName")
            .WhereIn("NodeGuid", ListGuid.Split('\n').Select(p => p.Trim()).ToList())
            .OrderBy(p => ListGuid.FindIndex(g => g == p.GUID)).ToList();

here is the test code:

        var guidOrder = new List<Guid>()
        {
            Guid.Parse("faa9c937-5900-4544-a659-a55fdf2f2e40"),
            Guid.Parse("16c3a215-38e5-4de8-8641-9587a6a6d710"),
            Guid.Parse("840c418c-0f93-4d33-842d-25b96a21545e"),
            Guid.Parse("6cb7778d-33e3-4bd5-afd3-c6e094d24026")
        };

        var listObjects = new List<MyListITem>()
        {
            new MyListITem() {Id = 1, GUID = Guid.Parse("840c418c-0f93-4d33-842d-25b96a21545e"), Name = "Peter"},
            new MyListITem() {Id = 2, GUID = Guid.Parse("faa9c937-5900-4544-a659-a55fdf2f2e40"), Name = "Alex"},
            new MyListITem() {Id = 3, GUID = Guid.Parse("16c3a215-38e5-4de8-8641-9587a6a6d710"), Name = "John"},
            new MyListITem() {Id = 4, GUID = Guid.Parse("6cb7778d-33e3-4bd5-afd3-c6e094d24026"), Name = "Boris"},
        };


        var orderded = listObjects.OrderBy(x => guidOrder.FindIndex(w => w == x.GUID)).ToList();


        orderded.ForEach(i => Console.Write("{0}\t{1}\t{2}\n", i.GUID, i.Id, i.Name));
0 votesVote for this answer Unmark Correct answer

Recent Answers


Roman Hutnyk answered on May 10, 2019 17:52

Try NodeOrder column. It will help you to order items the same way they appear in content tree. Is you move content item up or down in content tree it will change the order on presentation UI as well.

0 votesVote for this answer Mark as a Correct answer

Erick Valdivieso answered on May 10, 2019 19:42

Well that would take the possibility of rendering different orders for each feed widget, I am using the feed in different places and sometimes they require a different priority order.

Is there another option I could use to allow that?

0 votesVote for this answer Mark as a Correct answer

Erick Valdivieso answered on May 10, 2019 22:09

Interesting, I am going to give this a try. Thanks guys

0 votesVote for this answer Mark as a Correct answer

Erick Valdivieso answered on May 10, 2019 22:37

Sweet, it worked Peter. Here is my working snippet for reference:

    public ActionResult GetAllProducts(string ListGuid)
            {
                var guidOrder = new List<String>();
                foreach (var itemGuid in ListGuid.Split('\n').Select(p => p.Trim()).ToList())
                {
                    guidOrder.Add(itemGuid);
                }

                var listItems = ProductProvider.GetProducts()
                    .Columns("DocumentName", "NodeGUID", "ProductName")
                    .WhereIn("NodeGuid", guidOrder)
                    .OrderBy(p => guidOrder.FindIndex(g => g == p.NodeGUID.ToString())).ToList();

                var model = listItems.Select(item => new ProductViewModel()
                {
                    DocumentName = item.DocumentName,
                    ProductName = item.ProductName,
                    NodeGUID = item.NodeGUID,
                });

                return PartialView("DisplayTemplates/_ProductsAll", model);
}
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.