Click here to Skip to main content
       

C#

 
GeneralRe: DataGridView doesn't appear on screen PinmemberRickSharp21-Nov-12 13:27 
GeneralRe: DataGridView doesn't appear on screen PinmemberEddy Vluggen21-Nov-12 13:42 
QuestionTextbox help! PinmemberKurac121-Nov-12 11:18 
AnswerRe: Textbox help! PinmemberMycroft Holmes21-Nov-12 12:01 
QuestionSpecialized List Sorting question PinmemberNuclearMan8521-Nov-12 7:12 
AnswerRe: Specialized List Sorting question [modified] PinmemberRichard Deeming21-Nov-12 8:03 
AnswerRe: Specialized List Sorting question PinmemberSledgeHammer0121-Nov-12 8:06 
AnswerRe: Specialized List Sorting question PinmemberRichard Deeming21-Nov-12 8:32 
Option 2:
Wrap your lists with a class which applies the sort order where necessary:
public sealed class ListHelper<T> : IEnumerable<IList<T>>
{
   private int _sortIndex = -1;
   private IDictionary<int, int> _indexMap; // Source to target
   private IDictionary<int, int> _reverseIndexMap; // Target to source
   private readonly List<MappedList> _lists = new List<MappedList>();
   
   public int SortIndex
   {
      get
      {
         return _sortIndex;
      }
      set
      {
         if (value < -1 || value >= _lists.Count) 
         {
            throw new ArgumentOutOfRangeException();
         }
         
         if (-1 == value)
         {
            if (0 != _lists.Count)
            {
               int size = _lists[0].Count;
               _indexMap = Enumerable.Range(0, size).ToDictionary(i => i);
               _reverseIndexMap = _indexMap;
            }
         }
         else
         {
            IList<T> sortKey = _lists[value].InnerList;
            
            _indexMap = Enumerable.Range(0, sortKey.Count)
               .OrderBy(i => sortKey[i])
               .Select((key, v) => new { key, value = v })
               .ToDictionary(p => p.key, p => p.value);
            
            _reverseIndexMap = _indexMap.ToDictionary(p => p.Value, p => p.Key);
         }
      }
   }
   
   public int Count
   {
      get { return _lists.Count; }
   }
   
   public IList<T> this[int index]
   {
      get { return _lists[index]; }
   }
   
   public void Add(IList<T> listToAdd)
   {
      if (_indexMap == null)
      {
         _indexMap = Enumerable.Range(0, listToAdd.Count).ToDictionary(i => i);
         _reverseIndexMap = _indexMap;
      }
      else if (_indexMap.Count != listToAdd.Count)
      {
         throw new ArgumentException("Invalid list size!", "list");
      }
 
      _lists.Add(new MappedList(listToAdd, this));
   }
   
   public IEnumerator<IList<T>> GetEnumerator()
   {
      return _lists.GetEnumerator();
   }
   
   IEnumerator IEnumerable.GetEnumerator()
   {
      return GetEnumerator();
   }
   
   private sealed class MappedList : IList<T>
   {
      private readonly IList<T> _list;
      private readonly ListHelper<T> _parent;
      
      public MappedList(IList<T> list, ListHelper<T> parent)
      {
         _list = list;
         _parent = parent;;
      }
      
      public IList<T> InnerList
      {
         get { return _list; }
      }
      
      public T this[int index] 
      { 
         get
         {
            int realIndex = _parent._reverseIndexMap[index];
            return _list[realIndex];
         }
         set
         {
            int realIndex = _parent._reverseIndexMap[index];
            _list[realIndex] = value;
         }
      }
      
      public int Count
      {
         get { return _list.Count; }
      }
      
      public bool IsReadOnly
      {
         get { return _list.IsReadOnly; }
      }
      
      public IEnumerator<T> GetEnumerator()
      {
         for (int i = 0; i < _list.Count; i++)
         {
            int index = _parent._reverseIndexMap[i];
            yield return _list[index];
         }
      }
      
      IEnumerator IEnumerable.GetEnumerator()
      {
         return GetEnumerator();
      }
      
      public int IndexOf(T item)
      {
         int result = _list.IndexOf(item);
         return -1 == result ? -1 : _parent._indexMap[result];
      }
      
      public void CopyTo(T[] array, int arrayIndex)
      {
         foreach (T item in this)
         {
            array[arrayIndex++] = item;
         }
      }
      
      public bool Contains(T item)
      {
         return _list.Contains(item);
      }
      
      public void Add(T item)
      {
         throw new NotSupportedException();
      }
      
      public void Insert(int index, T item)
      {
         throw new NotSupportedException();
      }
      
      public bool Remove(T item)
      {
         throw new NotSupportedException();
      }
      
      public void RemoveAt(int index)
      {
         throw new NotSupportedException();
      }
      
      public void Clear()
      {
         throw new NotSupportedException();
      }
   }
}
 
...
 
var helper = new ListHelper<int>
{
   new[] { 04, 09, 03, 06 },
   new[] { 20, 50, 40, 30 },
   new[] { 15, 45, 85, 65 },
   new[] { 89, 39, 19, 99 },
};
 
helper.SortIndex = 0;
 
foreach (IList<T> list in helper)
{
   // Either foreach or for will work here:
   for (int index = 0; index < list.Count; index++)
   {
      // Each list will be sorted based on the order of the first list:
      Console.WriteLine(list[index]);
   }
}



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


AnswerRe: Specialized List Sorting question [modified] PinmemberPIEBALDconsult21-Nov-12 8:57 
AnswerRe: Specialized List Sorting question PinmemberBobJanova21-Nov-12 22:50 
QuestionWinForms Class Design PinmemberMember 961929521-Nov-12 6:43 
AnswerRe: WinForms Class Design PinmvpRichard MacCutchan21-Nov-12 7:15 
GeneralRe: WinForms Class Design PinmemberMatt U.21-Nov-12 8:16 
GeneralRe: WinForms Class Design PinmvpRichard MacCutchan21-Nov-12 8:56 
GeneralRe: WinForms Class Design PinmemberMatt U.21-Nov-12 8:59 
GeneralRe: WinForms Class Design PinmvpRichard MacCutchan21-Nov-12 9:06 
GeneralRe: WinForms Class Design PinmemberMatt U.21-Nov-12 9:21 
GeneralRe: WinForms Class Design PinmemberPIEBALDconsult21-Nov-12 10:27 
AnswerRe: WinForms Class Design PinmemberPIEBALDconsult21-Nov-12 9:07 
QuestionUplaod Article To Codeproject PingroupkatlegoEmmnanuelNkosi21-Nov-12 3:44 
AnswerRe: Uplaod Article To Codeproject PinmemberPIEBALDconsult21-Nov-12 3:48 
AnswerRe: Uplaod Article To Codeproject PinmvpRichard MacCutchan21-Nov-12 4:34 
QuestionPrint Crystal Report in c# Using Access Database? Pinmemberkashifjaat21-Nov-12 2:09 
QuestionGenerate Morse Code Sound Pinmemberlong dao21-Nov-12 1:24 
AnswerRe: Generate Morse Code Sound PinprotectorPete O'Hanlon21-Nov-12 1:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 25 Jul 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid