I have an interesting thought-problem right no开发者_如何学Pythonw, so maybe someone of you could help.
Basically what I have is an array of bytes and need to know the order of each individual item in that array - by value. Oh man, i will just show you a small example, i think that will help better:
byte[] array = new byte[4] { 42, 5, 17, 39 }
I want to get this as a result
4 1 2 3
What would be a smart way to do this?
In addition i would like the algorithm to retain the order, if the entries are valued equal. So
new byte[4] { 22, 33, 33, 11 }
should result in
2 3 4 1
You can use:
byte[] byteArray = new byte[4] { 42, 5, 17, 39 };
var results = byteArray.Select((b, i) => new { Value = b, Index = i})
.OrderBy(p => p.Value)
.Select((p, i) => new { Index = p.Index, SortedPosition = i + 1 })
.OrderBy(p => p.Index)
.Select(p => p.SortedPosition);
byte[] array = new byte[4] { 42, 5, 17, 39 }
var sorted = array.OrderBy(x=> x);
var result = from b in sorted
select (x => array.IndexOf(x) + 1);
As noted, this does not handle duplicates.
精彩评论