Click here to Skip to main content
   

C#

 
QuestionTextbox help! PinmemberKurac121-Nov-12 12:18 
AnswerRe: Textbox help! PinmemberMycroft Holmes21-Nov-12 13:01 
QuestionSpecialized List Sorting question PinmemberNuclearMan8521-Nov-12 8:12 
AnswerRe: Specialized List Sorting question [modified] PinmemberRichard Deeming21-Nov-12 9:03 
Option 1:
It's not clean, but if you want to sort the lists, this should work:
static void Sort<T>(IList<T> sortKey, params IList<T>[] otherLists)
{
   // TODO: Validate the arguments:
   // - None of the lists are null;
   // - All of the lists have the same length;

   Action<IList<T>, IDictionary<int, int>> sortList = (list, keys) =>
   {
      var values = list
         .Select((value, i) => new { key = keys[i], value })
         .OrderBy(p => p.key).Select(p => p.value);
 
      int index = 0;
      foreach (T value in values)
      {
         list[index++] = value;
      }
   };
 
   // Key is the index in the input list;
   // Value is the index in the output list.
   var indices = Enumerable.Range(0, sortKey.Count)
      .OrderBy(i => sortKey[i])
      .Select((key, value) => new { key, value })
      .ToDictionary(p => p.key, p => p.value);
 
   sortList(sortKey, indices);
   foreach (var list in otherLists)
   {
      sortList(list, indices);
   }
}
 
Edit:
Missed a ToList call in the anonymous delegate which would prevent this from working. Option 2 is probably a better approach. Smile | :)
 
Edit 2:
Nope, the ToList call isn't needed - the OrderBy call ensures that the entire sequence is evaluated on the first call to MoveNext. D'Oh! | :doh:
It still ends up creating a copy of the list, so option 2 is still better.



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



modified 22-Nov-12 7:15am.

AnswerRe: Specialized List Sorting question PinmemberSledgeHammer0121-Nov-12 9:06 
AnswerRe: Specialized List Sorting question PinmemberRichard Deeming21-Nov-12 9:32 
AnswerRe: Specialized List Sorting question [modified] PinmemberPIEBALDconsult21-Nov-12 9:57 
AnswerRe: Specialized List Sorting question PinmemberBobJanova21-Nov-12 23:50 
QuestionWinForms Class Design PinmemberMember 961929521-Nov-12 7:43 
AnswerRe: WinForms Class Design PinmvpRichard MacCutchan21-Nov-12 8:15 

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
Web04 | 2.8.150129.1 | Last Updated 1 Feb 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid