tags:

views:

62

answers:

2

Hi,

I have a collection of numbers starting from 1-9. I would like to reshuffle the collection so that the first object in the collection is 6 and everything before 6 is appended to the end of the list e.g. if if the first object is 3 than the collection would look like this: 345678912.

How can i do this using C# and linq?

Any help would be greatly appreciated.

Lee

+2  A: 

Something like:

return items.Skip(6).Concat(items.Take(6));

Where items is your collection and 6 is the number of items you want to move to the end.

Greg Beech
I think the expectation of the OP is not that you split at the sixth position, but rather that the value 6 is found wherever it appears in the array, and the array is split at that point, and reordered as indicated.
LBushkin
@LBushkin - The question is quite badly written so it's very hard to tell what the OP is actually asking. It's easy enough to use the same approach as this but with SkipWhile/TakeWhile instead of Skip/Take if that's what he means.
Greg Beech
+3  A: 

Try:

var coll = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

var shuffle = coll.SkipWhile( x => x != 6 )
                  .Concat( coll.TakeWhile( x => x != 6 ) )
                  .ToArray(); // converting to an array is optional....

// result: 6, 7, 8, 9, 1, 2, 3, 4, 5
LBushkin
Thanks LBushkin, worked a treat. I would also like to thank Greg as well for his alternate solution. Thanks Guys :-)
Lee