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
}
Source: stackoverflow.com
Related Articles
- Linq custom OrderBy using constants
- Custom sort logic in OrderBy using LINQ
- Linq OrderBy using a custom sort order
- Convert string[] to int[] in one line of code using LINQ
- Differences between IEquatable<T>, IEqualityComparer<T>, and overriding .Equals() when using LINQ on a custom object collection?
- Linq syntax for OrderBy with custom Comparer<T>
- Intersect with a custom IEqualityComparer using Linq
- How to OrderBy on a generic IEnumerable (IEnumerable<T>) using LINQ in C#?
- Workarounds for using custom methods/extension methods in LINQ to Entities
- EF and Linq OrderBy using two Parameters
- Using LINQ to Get Sum/ Average of a List with custom objects
- Convert string array to custom object list using linq
- Left outer join using LINQ -- understanding the code
- How to reuse a linq expression for 'Where' when using multiple source tables
- JIT error with LINQ OrderBy using C# on iOS
- Avoiding code repetition when using LINQ
- linq orderby using property name string
- Using LINQ to delete an element from a ObservableCollection Source
- LINQ OrderBy custom order
- LINQ Source Code Available
- multiple orderby in this linq code
- Linq - OrderBy the minimum value of two columns using lambda expressions?
- How can I write the following code more elegantly using LINQ query syntax?
- How can I code an outer join using LINQ and EF6?
- Custom Sorting using c# Linq Expressions
- How to OrderBy using Linq with missing elements?
- LINQ Lambda efficiency of code groupby orderby
- C# .Net 3.5 Code to replace a file extension using LINQ
- Linq orderby two-column sort with custom comparer
- LINQ - Sort List of objects using OrderBy EnumValue, and Union result set?
- ExpressionTree ExpressionVisitor to change/replace query OrderBy Field
- LINQ Order by - Parallel.Foreach
- Pagination Upper and Lower Limit
- How would I distinct my list of key/value pairs
- filtering list based on two different data columns linq C#
- Can we use ToLowerInvariant() in LINQ to SQL?
- Getting Profile ID of User from his Jobs using Sessions and LINQ
- How can i use a where clause within a select statement in linq. (ie. on a property.)
- LINQ in win32 DELPHI
- SQL statement converting to LINQ?
- Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`
- Selecting max revision with Linq statement
- Can I refactor my linq select?
- Get last several values from model
- Entity Framework Uninitialised Collection
- Setting visiblilty of control using LINQ
- Linq Exclude columns in children
- associated data in linq select
- Unwanted Change of Input in Func c#
- Entity Framework Query select hard-coded user created column