Click here to Skip to main content
15,887,350 members
Home / Discussions / C#
   

C#

 
GeneralRe: DataGridView doesn't appear on screen Pin
RickSharp21-Nov-12 13:27
RickSharp21-Nov-12 13:27 
GeneralRe: DataGridView doesn't appear on screen Pin
Eddy Vluggen21-Nov-12 13:42
professionalEddy Vluggen21-Nov-12 13:42 
QuestionTextbox help! Pin
Kurac121-Nov-12 11:18
Kurac121-Nov-12 11:18 
AnswerRe: Textbox help! Pin
Mycroft Holmes21-Nov-12 12:01
professionalMycroft Holmes21-Nov-12 12:01 
QuestionSpecialized List Sorting question Pin
NuclearMan8521-Nov-12 7:12
NuclearMan8521-Nov-12 7:12 
AnswerRe: Specialized List Sorting question Pin
Richard Deeming21-Nov-12 8:03
mveRichard Deeming21-Nov-12 8:03 
AnswerRe: Specialized List Sorting question Pin
SledgeHammer0121-Nov-12 8:06
SledgeHammer0121-Nov-12 8:06 
AnswerRe: Specialized List Sorting question Pin
Richard Deeming21-Nov-12 8:32
mveRichard 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 Pin
PIEBALDconsult21-Nov-12 8:57
mvePIEBALDconsult21-Nov-12 8:57 
AnswerRe: Specialized List Sorting question Pin
BobJanova21-Nov-12 22:50
BobJanova21-Nov-12 22:50 
QuestionWinForms Class Design Pin
Member 961929521-Nov-12 6:43
Member 961929521-Nov-12 6:43 
AnswerRe: WinForms Class Design Pin
Richard MacCutchan21-Nov-12 7:15
mveRichard MacCutchan21-Nov-12 7:15 
GeneralRe: WinForms Class Design Pin
Matt U.21-Nov-12 8:16
Matt U.21-Nov-12 8:16 
GeneralRe: WinForms Class Design Pin
Richard MacCutchan21-Nov-12 8:56
mveRichard MacCutchan21-Nov-12 8:56 
GeneralRe: WinForms Class Design Pin
Matt U.21-Nov-12 8:59
Matt U.21-Nov-12 8:59 
GeneralRe: WinForms Class Design Pin
Richard MacCutchan21-Nov-12 9:06
mveRichard MacCutchan21-Nov-12 9:06 
GeneralRe: WinForms Class Design Pin
Matt U.21-Nov-12 9:21
Matt U.21-Nov-12 9:21 
GeneralRe: WinForms Class Design Pin
PIEBALDconsult21-Nov-12 10:27
mvePIEBALDconsult21-Nov-12 10:27 
AnswerRe: WinForms Class Design Pin
PIEBALDconsult21-Nov-12 9:07
mvePIEBALDconsult21-Nov-12 9:07 
QuestionUplaod Article To Codeproject Pin
katlegoEmmnanuelNkosi21-Nov-12 3:44
katlegoEmmnanuelNkosi21-Nov-12 3:44 
AnswerRe: Uplaod Article To Codeproject Pin
PIEBALDconsult21-Nov-12 3:48
mvePIEBALDconsult21-Nov-12 3:48 
AnswerRe: Uplaod Article To Codeproject Pin
Richard MacCutchan21-Nov-12 4:34
mveRichard MacCutchan21-Nov-12 4:34 
QuestionPrint Crystal Report in c# Using Access Database? Pin
kashifjaat21-Nov-12 2:09
kashifjaat21-Nov-12 2:09 
QuestionGenerate Morse Code Sound Pin
long dao21-Nov-12 1:24
long dao21-Nov-12 1:24 
AnswerRe: Generate Morse Code Sound Pin
Pete O'Hanlon21-Nov-12 1:45
mvePete O'Hanlon21-Nov-12 1:45 

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

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