0

I want to select array values from specific indexes Now I have this.

var xs = new[] { 11,12,13,14,15 };
var ind = new[] { 3,2,1,0 };
var results = xs.Where((x, idx) => ind.Contains(idx)).ToArray();

The result is {11,12,13,14} However, I want my result to be ordered by index array which should be {14,13,12,11}

Thank you very much

2 Answers 2

3
var results = ind.Select(i => xs[i]).ToArray();
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry. I accidentally hit the wrong vote. Why couldn't I figure this out. Thanks a lot man
No worries dude. Linq is a joy when you start to get a feel for it.
Thanks. I wish I could enjoy it. It's a pain in the ass for me now
0
var array = xs.Zip(ind, (x, i) => new Tuple<int, int>(x, i))
           .OrderBy(t => t.Item2)
           .Select(t => t.Item1)
           .ToArray();

Comments

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.