0

我正在尝试修改已经存在的代码。代码是在 2008 年左右开发的,我正在尝试解决排序问题。我也在考虑更改代码,但想先解决这个问题。

ArrayList lineEmpNrs = new ArrayList();
taMibMsftEmpDetails employeeDetails; //taMibMsftEmpDetails is a custom class file

while (!HeaderFile.EndOfStream) //headerfile is employee information text file.
{
    headerRow = HeaderFile.ReadLine();
    headerFields = headerRow.Split(',');
    employeeDetails = BuildLine(headerFields[1],headerFields[2],headerFields[3]);
    lineEmpNrs.Add(employeeDetails);
}

 private taMibMsftEmpDetails BuildLine(string EmpId, string EmpName, String ExpnsDate)
 {
     taMibMsftEmpDetails empSlNr = new taMibMsftEmpDetails();
     empSlNr.EmployeeId  = EmpId;
     empSlNr.EmployeeName   = EmpName;
     empSlNr.ExpenseDate = ExpnsDate;
     return empSlNr;
 }

头文件包含员工费用详细信息。empID 是这里的关键,headerFile 可以包含“n”行具有相同 EmpID 的行,这些行以随机顺序出现在文件中。

我使用 lineEmpNrs 来构建基于 EmpID 的其他一些信息。所以我想根据 EmpID 对 lineEmpNrs 进行排序。我尝试了常规的排序方法,但没有奏效。

请建议。

4

6 回答 6

3

首先-因此它是.Net 2.0,您可以使用强类型的通用类型列表List<taMibMsftEmpDetails>而不是ArrayList

List<taMibMsftEmpDetails> lineEmpNrs = new List<taMibMsftEmpDetails>();
// rest is same

其次 - 您可以使用List<T>.SortComparison<T>委托的方法按 id 对员工进行排序:

lineEmpNrs.Sort(delegate(taMibMsftEmpDetails e1, taMibMsftEmpDetails e2) { 
                   return e1.EmployeeId.CompareTo(e2.EmployeeId); 
                });

您可以创建常用的命名方法来比较员工,而不是匿名方法:

lineEmpNrs.Sort(CompareEmployees);

还要考虑改进代码中的命名。考虑使用employeescollection 而不是lineEmpNrsEmployeeDetailsclass 而不是taMibMsftEmpDetails.

于 2013-07-26T16:49:46.913 回答
2

如下所述,这不适用于 ArrayList,但您可以转换lineEmpNrsList<taMibMsftEmpDetails>.

LINQ 在这里是完美的。

var sortedList = lineEmpNrs.OrderBy(emp => emp.EmployeeId);

看到您的评论后,这只有在您可以将代码从 .NET 2.0 升级到 3.5 或更高版本时才有效。

于 2013-07-26T16:33:14.260 回答
2

此代码是 .Net 2.0 安全的:

public static string ReadLines(StreamReader input)
{
    string line;
    while ( (line = input.ReadLine()) != null)
       yield return line;
}

private taMibMsftEmpDetails BuildLine(string EmpId, string EmpName, String ExpnsDate)
{
    taMibMsftEmpDetails empSlNr = new taMibMsftEmpDetails();
    empSlNr.EmployeeId  = EmpId;
    empSlNr.EmployeeName   = EmpName;
    empSlNr.ExpenseDate = ExpnsDate;
    return empSlNr;
}

List<taMibMsftEmpDetails> lineEmpNrs = new List<taMibMsftEmpDetails>();
foreach (string line in ReadLines(HeaderFile))
{
    headerFields = line.Split(',');
    lineEmpNrs.Add(BuildLine(headerFields[1],headerFields[2],headerFields[3]));
}
lineEmpNrs.Sort(delegate(taMibMsftEmpDetails a, taMibMsftEmpDetails a) 
   { 
      return a.EmployeeId.CompareTo(a.EmployeeId); 
   });    

如果您至少可以达到 .Net 3.5(仍然使用 .Net 2.0 运行时),它就会变得简单得多:

public static string ReadLines(StreamReader input)
{
    string line;
    while ( (line = input.ReadLine()) != null)
       yield return line;
}

var lineEmpNrs = ReadLines(HeaderFile)
   .Select(l => l.Split(','))
   .Select(l => new taMibMsftEmpDetails() 
       {
          EmployeeId   = l[1],
          EmployeeName = l[2],
          ExpenseDate  = l[3]
       })
   .OrderBy(l=> l.EmployeeId)
   .ToList();
于 2013-07-26T16:57:44.057 回答
1

您可以将 ArrayList 用作列表

var enumerableCollection = from taMibMsftEmpDetails ln in lineEmpNrs
                                        orderby ln.EmployeeId
                                        select ln;

您可以转换为Array或使用List

于 2013-07-26T16:44:39.640 回答
1

您还可以将数据加载到 DataTable 并通过 DataView 进行排序。

    DataTable dt = new DataTable();
    dt.Columns.Add("EmpID");
    dt.Columns.Add("Field2");
    dt.Columns.Add("Field3");

    // Read file, split values, add to table
    while (!HeaderFile.EndOfStream) {

        headerRow  = HeaderFile.ReadLine();
        headerFields = headerRow.Split(',');

        // Create row and add it to the table
        DataRow dr = dt.NewRow();
        dr["EmpID"] = headerFields[0];
        dr["Field1"] = headerFields[1];
        dr["Field2"] = headerFields[2];
        dt.ImportRow(dr);
    }

    // Sort table by EmpID
    DataView dv = dt.DefaultView;
    dv.Sort = "EmpID ASC";
    dt = dv.ToTable();
于 2013-07-26T16:52:21.363 回答
1

常规排序方法应该与自定义比较方法一起使用,如下所示:

private class sortByEmployeeIdHelper: IComparer
{
   int IComparer.Compare(object a, object b)
   {
       taMibMsftEmpDetails employeeA = (taMibMsftEmpDetails)a;
       taMibMsftEmpDetails employeeB = (taMibMsftEmpDetails)b;

       //employee id is numeric?
       int id = int.Parse(employeeA.EmployeeId);
       return id.CompareTo(int.Parse(employeeB.EmployeeId));
   }
}

IComparer myComparer = new sortByEmployeeIdHelper();
lineEmpNrs.Sort(myComparer);
于 2013-07-26T18:09:12.810 回答