Carl
Carl

Reputation: 324

Linq custom OrderBy using constants

I am attempting to sort a list of items that have an ID, but I would like to implement a custom sort order based on constants. The following works, but I am not a big fan of the lambda expression. Is there a better way to do this?

using System.Collections.Generic;
using System.Linq;
using System;

public class Program
{
    public static void Main()
    {
        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(list => list.SomeId == SomeIdConstants.FIRST 
            ? 1 
            : list.SomeId == SomeIdConstants.SECOND 
              ? 2 
              : list.SomeId == SomeIdConstants.THIRD 
                ? 3 
                : list.SomeId == SomeIdConstants.FOURTH 
                  ? 4 : 5);

        foreach (var listItem in orderedList)
        {
            Console.WriteLine(listItem.SomeId);
        }
    }
}

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

public class SomeIdConstants
{
    public const int FIRST = 2;
    public const int SECOND = 1;
    public const int THIRD = 4;
    public const int FOURTH = 5;
    public const int FIFTH = 3;
}

Upvotes: 3

Views: 726

Answers (3)

Jonathan Twite
Jonathan Twite

Reputation: 952

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
}

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460228

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));

Upvotes: 5

sloth
sloth

Reputation: 101132

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).

Upvotes: 8

Related Questions