Adelaiglesia
Adelaiglesia

Reputation: 365

Order list in Certain Order in C# .NET

I have a list made ​​up from the following class:

public class Category
        {
            public int id { get; set; }
            public string slug { get; set; }
            public string title { get; set; }
            public string description { get; set; }
            public int parent { get; set; }
            public int post_count { get; set; }
        }

Here is the list.

List<Category> cats = new List<Category>();

I wish I could sort it according to the id (int), but in a certain order i have:

3, 17, 13, 176, 4, 177, 179, 178, 180, 181, 182, 183, 184, 185, 186, 139

Is there any way to do this?

Thank you.

Upvotes: 0

Views: 261

Answers (4)

Grant Winney
Grant Winney

Reputation: 66439

Create a custom Comparer:

public class CategoryComparer : IComparer<Category>
{
    List<int> orderNums =
        new List<int> { 3, 17, 13, 176, 4, 177, 179, 178, 180, 181, 182, 183, 184, 185, 186, 139 };

    public int Compare(Category x, Category y)
    {
        var index1 = orderNums.IndexOf(x.id);
        var index2 = orderNums.IndexOf(y.id);

        if (index1 == index2)
            return 0;

        return index1 > index2 ? 1 : -1;
    }
}

Then Sort the list using the new Comparer:

cats.Sort(new CategoryComparer());

Or use LINQ to leave the original list untouched:

var sortedCats = cats.OrderBy(x => x, new CategoryComparer());

Upvotes: 0

Saverio Terracciano
Saverio Terracciano

Reputation: 3915

There are a few ways to do this:

Upvotes: 1

Ann L.
Ann L.

Reputation: 13965

OK, I'm assuming that what you mean is that you'd like to sort in the order

3, 17, 13, 176, 4, 177, 179, 178, 180, 181, 182, 183, 184, 185, 186, 139

... rather than in strict numeric order.

Can you add a SortOrder field to the class?

If you can't, you might be able to emulate one. Create a class that has two properties, ID and SortOrder:

public class CategorySortOrder
{
    int CategoryId { get;set;}
    int SortOrder { get;set;}
}

Create an array of those, in the right order:

var catSortOrder = new[] {
   new CategorySortOrder() { CategoryId = 3, SortOrder = 1 },
   new CategorySortOrder() { CategoryId = 17, SortOrder = 2 }
   // etc.
   }

Then execute some LINQ:

var catList = from cat in cats 
              join so in catSortOrder on cat.id = so.CategoryId
              select new {
                cat = cat,
                sort_order = so.SortOrder
              };
var finalist = catList
                  .OrderBy(item => item.SortOrder)
                  .Select(item => item.cat)
                  .ToList();

If all you mean is that you want the IDs to be in numeric order, you can sort them with LINQ, like this:

var catList = cats.OrderBy(cat => cat.id).ToList();

Upvotes: 3

Ufuk Hacıoğulları
Ufuk Hacıoğulları

Reputation: 38468

int[] orderData = new[] {3, 17, 13, 176, 4, 177, 179, 178, 180, 181, 182, 183, 184, 185, 186, 139};
List<Category> orderedCategories = cats.OrderBy(item => Array.IndexOf(orderData, item.id)).ToList();

Upvotes: 1

Related Questions