title: c# 2D array bubblesort
body: <p>I am trying to perform a bubble sort on a 2D array, sorting by the third index (the integer)</p>
  
  <pre><code>string[,] edges = new string[,] { {"A", "B", "2"},
                                            {"A", "C", "3"},
                                            {"A", "E", "10"},
                                            {"B", "C", "5"},
                                            {"B", "D", "10"},
                                            {"C", "D", "2"},
                                            {"D", "E", "5"},
                                            {"E", "B", "3"}
                                          }; 
  </code></pre>
  
  <p>I get an IndexOutOfRangeException on IF statenent of the sort code</p>
  
  <pre><code>        string[] temp = {};
  
          //sort edges and add them to sortedEdges - using bubblesort
          for (int i = 0; i &lt; edges.Length - 1; i++){
              for (int j = 0; j &lt; edges.Length - 1; j++){
                  if (Int32.Parse(edges[i, 2]) &gt; Int32.Parse(edges[i + 1, 2])){
                      //make a swap
                      //put array record i into temp holder
                      temp[0] = edges[i, 0];
                      temp[1] = edges[i, 1];
                      temp[2] = edges[i, 2];
  
                      //copy i + 1 into i
                      edges[i, 0] = edges[i + 1, 0];
                      edges[i, 1] = edges[i + 1, 1];
                      edges[i, 2] = edges[i + 1, 2];
  
                      //copy temp into i + 1
                      edges[i + 1, 0] = temp[0];
                      edges[i + 1, 1] = temp[1];
                      edges[i + 1, 2] = temp[2];
  
                  }
              }
          }
  </code></pre>
  
  <p>My question is, how do I fix this so that the array "edges" is filled with the rows, ordered by the third column?</p>
  
  <p>Thanks.</p>