Ordering Records in a non-standard way with a dataset

I needed to order records but there was no way to order it based upon setting the sort method to order it. I had to create a method that would sort the records based upon the values that were out of order based upon the sort. This is the method that was used:

private DataSet OrderRecords(DataSet ds, string fieldName, string[] values) {

  DataSet retVal = new DataSet();
  DataTable dt = ds.Tables[0].Copy();
  DataRow[] drArray = new DataRow[values.Length];
  dt.Rows.CopyTo(drArray, 0);
  ds.Tables[0].Clear();

  foreach (string s in values) {
    foreach (DataRow dr in drArray) {
      if (dr[fieldName].ToString().IndexOf(s) != -1) {
        ds.Tables[0].ImportRow(dr);
        break;
      }
    }
  }

  retVal = ds;
  return retVal;
}

Comments are closed.