How to sort multi-dimensional array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dimon
    New Member
    • Jul 2007
    • 18

    How to sort multi-dimensional array?

    Hi there,
    I need to sort multi-dimensional array by two columns. I.e. I have the following:

    a b c
    -----------
    1 12 1
    0 10 2
    1 10 3
    1 15 4
    0 14 5

    So the result should look like this:

    a b c
    -----------
    1 15 4
    1 12 1
    1 10 3
    0 14 5
    0 10 2

    As you can see I need to order it in descending order by column A first and then by column B so that column C should still be mapped appropriately.

    When I had just 2 columns (B and C) it was pretty easy to do:

    Hashtable HTPreferenceOrd er = new Hashtable();
    //next line is in loop
    HTPreferenceOrd er.Add(B,C);
    //end loop
    int count= HTPreferenceOrd er .Count;
    int[] B= new int[count];
    int[] C= new int[count];
    HTPreferenceOrd er.Keys.CopyTo( B,0);
    HTPreferenceOrd er.Values.CopyT o(C, 0);
    Array.Sort(B, C);
    Array.Reverse(C );

    That's it. I got column C in descending order like i wanted it to be. But now i had to add column A and sort by it first. Please help, need to do it ASAP.

    Thanks in advance.
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Have you tried doing the same with column A?

    Comment

    • Dimon
      New Member
      • Jul 2007
      • 18

      #3
      Originally posted by kenobewan
      Have you tried doing the same with column A?
      Do what with column A? I can't do it with column A because i need it to be sorted by column A and column B and so column C is still mapped appropriately.

      Comment

      Working...