解决方案 »

  1.   

    lines.Cast<Line>().OrderBy(x => x.storeNum)
      

  2.   

    HashSet的内容如何排序
      

  3.   

    考虑一下SortedSet<T> 否?
      

  4.   

    曹大,OrderBy好像不能对lines内部结构的一部分字段排序吧,要不你测试,测试代码:
    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.Linq;
    using System.Text;
    using System.IO;  namespace ValidReplicatedLine
    {
        class Program
        {
            static void Main(string[] args)
            {
                string inputFileName = @"E:\inputfile.txt";
                StreamReader sr = new StreamReader(inputFileName);
                HashSet<Line> lines = new HashSet<Line>();
                int lineNum = 1;
     
                while (!sr.EndOfStream)
                {
                    string str = sr.ReadLine();                if (str.Trim().Length == 0)
                    {
                        continue;
                    }                string[] strArr = str.Split(new char[] { ',' });
                    Line line = new Line();
                    line.lineNum = lineNum;
                    line.storeNum = strArr[0];
                    line.MosrName = strArr[1];
                    line.mosrNum = strArr[2];                lines.Add(line);
                    lineNum++;      
                }            sr.Close();
                
                lines.Cast<Line>().OrderBy(x => x.storeNum);            foreach(Line l in lines)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine(Convert.ToString(l.lineNum) + ',' + l.storeNum + ',' + l.MosrName + ',' + l.mosrNum);
                    Console.Write(sb);
                }
            }
            public class Line
            {
                 public int lineNum;
                 public string storeNum;
                 public string MosrName;
                 public string mosrNum;
            }
        }
    }
    运行结果:
      

  5.   

    测试通过,hashset.OrderBy()是返回一个排序后的副本,多谢曹大,待会接分。
      

  6.   

    你的思路也行,将HashSet转换成ToList(),再利用OrderBy()自定义比较器。多谢