Order IEnumerable without LINQ
문제
Can you advice any solution of how to sort IEnumerable<byte> indexes
in .NET 3.0 (no LINQ)?
Of course it is possible to determine indexes
length, create array, copy element-by-element, then call Array.Sort(array)
. But may be can you suggest anything else?
해결책
As long as you aren't using the 2.0 compiler (so: VS 2008 / 2010 / 2012), you can use LINQBridge, and use LINQ-to-Objects from your .NET 2.0/3.0 code.
The other lazy solution is:
List<byte> list = new List<byte>(indexes);
list.Sort();
// list is now a sorted clone of the data
다른 팁
Don't think there is any other solution then iterating over "manually", in C# 2.0
Another option of creating array.
You can create a List<>
var list = new List<byte>(indexes );
list.Sort(delegate(byte b1, byte b2)
{
//your comparison logic here
});
It's more compact then simple for
or foreach
iteration over collection.
The entire IEnumerable<>
has to be read when you sort it, so there is no way around that. Even the Linq to Objects method Sort
keeps the entire collection in memory.
Create a List<byte>
from the IEnumerable<byte>
and sort it:
List<byte> list = new List<byte>(indexes);
list.Sort();
Since you can't really change an IEnumerable, you're going to have to copy the data somewhere else to sort it.
However, note you're sorting bytes, you can use Bucket Sort for ultra-efficient sorting.
http://www.codeproject.com/Articles/80546/Comparison-Sorting-Algorithms-in-C-Explained
This came in handy when i was on a search for a solution