score:5

Accepted answer

You could put them into an ordered collection:

int[] orderConstants = { SomeIdConstants.FIRST, SomeIdConstants.SECOND, SomeIdConstants.THIRD, SomeIdConstants.FOURTH, SomeIdConstants.FIFTH };

then you can use Array.IndexOf (if you've put them into a list use List.IndexOf):

var orderedList = myList.OrderBy(i => Array.IndexOf(orderConstants, i.SomeId));

score:8

You could use a List instead, like

var lookup = new List<int> {2, 1, 4, 5, 3};
var orderedList = myList.OrderBy(list => lookup.IndexOf(list.SomeId));

This puts elements with a SomeId that's not in lookup at the front (this may or may not be an issue for you. If it is, it's trivial to handle this).

score:0

Using a Custom Comparer

EDIT: using enum for order

class Program
{
    static void Main(string[] args)
    {
        var myList = new List<MyItem>
        { 
            new MyItem { SomeId = 1 },
            new MyItem { SomeId = 2 },
            new MyItem { SomeId = 3 },
            new MyItem { SomeId = 4 },
            new MyItem { SomeId = 5 }
        };

        var orderedList = myList.OrderBy(d => d, new MyItemComparer());
        foreach (var listItem in orderedList)
        {
            Console.WriteLine(listItem.SomeId);
        }
        Console.ReadLine();
    }
}

public class MyItem
{
    public int SomeId { get; set; }
}

public class MyItemComparer : IComparer<MyItem>
{
    public int Compare(MyItem i1, MyItem i2)
    {
        int pos1 = pos(i1);
        int pos2 = pos(i2);

        if (pos1 > pos2) { return 1; }
        if (pos1 < pos2) { return -1; }
        return 0;
    }

    // Return the required position for a value of SomeId
    private int pos(MyItem it)
    {
        switch (it.SomeId)
        {
            case (int)SomeIdConstants.FIRST: return 1;
            case (int)SomeIdConstants.SECOND: return 2;
            case (int)SomeIdConstants.THIRD: return 3;
            case (int)SomeIdConstants.FOURTH: return 4;
            case (int)SomeIdConstants.FIFTH: return 5;
            default: return 999;
        }
    }
}

public enum SomeIdConstants : int
{
    FIRST = 2,
    SECOND = 1,
    THIRD = 4,
    FOURTH = 5,
    FIFTH = 3
}

Related Articles