Enums can have an integer value (some might call it an "ordinal") assigned to them. You can get this integer value by casting.
int valueOfEnum = (int)SomeEnum.SomeEnumValue;
In your case, you haven't assigned the integer values, which is just fine. If you don't assign them, then C# will automatically assign them for you. They start at 0
, and increment from there.
Here's the MSDN documentation on using these enum "values".
You can put the items in your enum SomeAction
definition in the order you want them to occur, and let C# assign the values automatically. This might be all that you need to do.
You could also assign values to each of them that specify the order that you prefer. This can help if you want to declare them in a different order.
public enum SomeAction
{
Read = 1,
Create = 3,
Update = 2, // Update should come before Create in my list, so I give it a lower value
// ...
If you want to sort actions by groups, you could use this manual assignment technique to assign duplicate values (perhaps Archive = 1
and Update = 1
).
Now that you have a basis for comparing items in your list, there are a few ways to go about ensuring that they are in order:
Just sort it
A great way to ensure a constraint is to do the work yourself. Sort it, and it will be sorted :)
This will only work if your sorting function produces a stable sort. The documentation on MSDN says OrderBy
does stable sorting so you're fine if you use this method.
It will also only work if you can afford to re-order the items in your list. Judging by the definition of your actions (a list of actions that are each dependent on previous state), I am not sure this is true. You might need to pick one of the other methods for checking order.
var sortedList = yourList.OrderBy(item => (int)item.Action).ToList();
Compare the list to a sorted version of itself
This is useful if you are doing error checking, but don't want to correct the error.
This method won't change the list, so it is probably safe for the type of actions you're looking at.
var sortedList = yourList.OrderBy(item => (int)item.Action);
bool isSorted = Enumerable.SequenceEqual(yourList, sortedList);
Write a comparison algorithm manually
This may be useful if you need to tightly control the memory allocation and CPU usage.
You probably don't need this level of control unless your list is really big, or you're in the middle of a tight loop in high performance code (like a video game draw loop). Even in those cases, you may want to consider refactoring the code so this check isn't in the middle of a tight loop, if it is possible.
For why I used for
instead of foreach
, see - In .NET, which loop runs faster, 'for' or 'foreach'?
// Note that you have to build a custom type for your list items to keep high performance,
// or write this inline instead of as a function, to avoid the perf hit of IList<dynamic>
bool IsMySpecificListTypeSorted(List<MyCustomListItemType> theList) {
int previousOrdinal = -1;
// Not using foreach because it is "8x slower" in tests
// and you're micro-optimizing in this scenario
for(int index = 0; index < theList.Count; ++index) {
var item = theList[index];
var currentOrdinal = (int)item.Action;
if(currentOrdinal < previousOrdinal) {
return false;
}
previousOrdinal = currentOrdinal;
}
return true;
}