0

i am not very familiar with linq syntax, and i am not sure if this query is the perfect fit of what i am trying to achieve but its working perfectly. how can i make it better and is it possible avoiding the use of a new tuple or a new object?

byte[] ShuffledBytes= new byte[20];
byte[] Indecies = new byte[20]; //holds the right index for each byte in Bytes

    var orderdBytes = 
                    Indecies.
                    Zip(ShuffledBytes, (i, b) => new Tuple<byte,byte>(b,i)).
                    OrderBy(o => o.Item2).
                    Select(o => o.Item1).
                    ToArray();
2
  • If you do not want to transform your result into a list of some kind of objects, what do you expect the linq statement to do? Commented Jan 4, 2014 at 1:38
  • @MichaC its just that i am creating an object just for ordering, and in the end result i just want a byte[], also was wondering if anyone would do it the same way Commented Jan 4, 2014 at 1:39

1 Answer 1

6

I think want you want is Array.Sort rather than LINQ. One of the overloads uses one array as the key and the other as the items to sort:

Array.Sort(Indecies, ShuffledBytes);

No extra objects needed ShuffledBytes is sorted according to the values in Indecies.

Another option would be to use a sorted dictionary or a list of keyvaluepair's, rather than 2 arrays.

Sign up to request clarification or add additional context in comments.

1 Comment

woah thats just perfect!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.