Click here to Skip to main content
15,886,873 members
Home / Discussions / C#
   

C#

 
QuestionDataGridView doesn't appear on screen Pin
RickSharp21-Nov-12 12:44
RickSharp21-Nov-12 12:44 
AnswerRe: DataGridView doesn't appear on screen Pin
Eddy Vluggen21-Nov-12 13:17
professionalEddy Vluggen21-Nov-12 13:17 
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 
Option 1:
It's not clean, but if you want to sort the lists, this should work:
C#
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 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 
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 

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.