2015-01-06 38 views
-4

我已經通過INT(這是關鍵)訂購了KeyValuePair列表訂購KeyValuePair列表,所以我有一個序列:以特定方式

01, 02, 03, 04, 05, 06, 07, 08, 09, 10... 

我必須要改變的順序我元素(6分頁6):

01, 04, 02, 05, 03, 06, 
07, 10, 08, 11, 09, 12, 
13, 16, 14, 17, 15, 18... 

以單位時間內的六大要素應該足夠總結3至各個指標,但我不能元素回遷和FORW。有任何想法嗎?

+0

我真的不明白你想要什麼。你需要什麼樣的特定類型,你想要它是什麼樣的? – Jonesopolis

+0

我不明白這句話:「每次拿六個元素應該足以把每個索引加起來3」 –

+0

@ L.B這真的不是重複嗎?他想重新排列元素,而不是批量處理它們。示例看起來像一批,因爲這是他如何佈置它,但它仍然是一個單一的列表。 –

回答

2

您可以partition列表中6個元素的序列,那麼你interleave第3個元素爲每個6元序列的最後3個元素。

像這樣:

using System; 
using System.Collections.Generic; 
using System.Linq; 

public static class MyExtentions 
{ 
    public static IEnumerable<T> Interleave<T>(
     this IEnumerable<T> first, 
     IEnumerable<T> second) 
    { 
     using (IEnumerator<T> 
      enumerator1 = first.GetEnumerator(), 
      enumerator2 = second.GetEnumerator()) 
     { 
      while (enumerator1.MoveNext()) 
      { 
       yield return enumerator1.Current; 
       if (enumerator2.MoveNext()) 
        yield return enumerator2.Current; 
      } 
     } 
    } 

    public static IEnumerable<List<T>> Partition<T>(this IEnumerable<T> source, int max) 
    { 
     List<T> toReturn = new List<T>(max); 
     foreach(var item in source) 
     { 
       toReturn.Add(item); 
       if (toReturn.Count == max) 
       { 
         yield return toReturn; 
         toReturn = new List<T>(max); 
       } 
     } 
     if (toReturn.Any()) 
     { 
       yield return toReturn; 
     } 
    } 
} 


public class Program 
{ 
    public static void Main() 
    { 
     var sequence = Enumerable.Range(1, 18); 
     var result = sequence 
         .Partition(6) 
         .Select(s => { 
           var firstThree = s.Take(3); 
           var lastThree = s.Skip(3); 
           return firstThree.Interleave(lastThree); 
          }) 
         .SelectMany(x => x); 
     foreach(var i in result){ 
      Console.WriteLine(i); 
     } 
    } 
} 

我也把一個fiddle

1

如果你做了這樣的事情怎麼辦?首先應用分頁,然後按照您希望的方式專門訂購您的鑰匙。

List<int> keys = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }; 

int page = 1; 
int keysPerPage = 6; 
// Apply paging to the keys. 
var pagedKeys = keys.Skip((page - 1) * keysPerPage).Take(keysPerPage).ToList(); 

// Then, take the keys in the order of 0,3,1,4,2,5 
var orderedKeys = new List<int>() { pagedKeys[0], pagedKeys[3], pagedKeys[1], pagedKeys[4], pagedKeys[2], pagedKeys[5] };