-2

THX ALL OF YOU .USING LIST<> INSTEAD ARRAYLIST IS RLY HELPFULL

So i heave a struct, wich contain information about worker, fio-name of worker, dolj-his job, staj-how much he working on this job and zp-how much money he takes, this info i keep in file with strings, expl:Nick, road-worker, 5,200. I have 8 thoose string like expl, and i need to sort them by the name of worker, using arraylist and struct, plz help !

//Struct
 public struct Worker
{
    public string FIO;
    public string Dolj;
    public int Staj;
    public int Zp;
    public Worker(string fi, string dj, int st, int zrp)
    {
        FIO = fi;
        Dolj = dj;
        Staj = st;
        Zp = zrp;
    }
//reading from file

ArrayList listw = new ArrayList();
            Worker W;
            StreamReader file= new StreamReader("Workers.txt", Encoding.Default);
          ;
            while (!file.EndOfStream)
            {
                string[] wtemp = file.ReadLine().Split(',');
                W = new Worker(wtemp[0],wtemp[1],Convert.ToInt32(wtemp[2]),Convert.ToInt32(wtemp[3]));
                listw.Add(W);
            }

//display listw
//!!!!! So here i need to sort arraylist or struct by FIO (name of worker)
// Arraylist - listw 
// (Name,job,years,money)
// (Name,job,years,money) --Sort by name!

 foreach (Worker outworker in listw)
            {

                Console.WriteLine
                (outworker.FIO,outworker.Dolj,outworker.Staj,outworker.Zp);
            }
  • Don't use `ArrayList`, use the generic type `List`. – BlueM Apr 29 '14 at 14:39
  • 2
    By using Linq you can use `listw.OrderBy(w => w.FIO)` – devqon Apr 29 '14 at 14:40
  • 1
    Use a `List` instead of an ArrayList. Make `Worker` a class that implements `IComparable` and define how two workers should be compared (i.e. sorted). Then you can use the Sort method to sort the list easily. – John Willemse Apr 29 '14 at 14:41
  • http://stackoverflow.com/questions/3309188/how-to-sort-a-listt-by-a-property-in-the-object – jspurim Apr 29 '14 at 14:45

3 Answers3

3

You can use LINQ:

var orderedWorkers = listw.OfType<Worker>().OrderBy(x => x.FIO).ToList();

Also always use List<T> class instead of ArrayList, for type safety.

Another alternative is implementing a custom Comparer for your type:

public struct NameComparer : IComparer
{
    public int Compare(object x, object y)
    {
        if (x is Worker && y is Worker)
        {
            return ((Worker)x).FIO.CompareTo(((Worker)y).FIO);
        }
        return 0;
    }
}

Then just call Sort method:

 listw.Sort(new NameComparer());
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
0

Change your listw type to List<Worker> and you can do a LINQ sort easily.

foreach (var outworker in listw.OrderBy(worker => worker.FIO))
{
    Console.WriteLine(outworker.FIO, outworker.Dolj, outworker.Staj, outworker.Zp);
}
BlueM
  • 6,523
  • 1
  • 25
  • 30
  • `ArrayList` doesn't implement `IEnumerable`, you can't call linq methods directly on ArrayList. – Selman Genç Apr 29 '14 at 14:42
  • Thats why I said change to `List` as I recommended in my comment below his question. ArrayList shouldn't be used at all nowadays. – BlueM Apr 29 '14 at 14:43
0

//reading from file

List<Worker> listw = new List<Worker>();
Worker W;
StreamReader file= new StreamReader("Workers.txt", Encoding.Default);

while (!file.EndOfStream)
{
    string[] wtemp = file.ReadLine().Split(',');
    W = new Worker(wtemp[0],wtemp[1],Convert.ToInt32(wtemp[2]),Convert.ToInt32(wtemp[3]));
    listw.Add(W);
}

//Sort by name.
listw = listw.OrderBy(x=> x.FIO).ToList();

foreach (Worker outworker in listw)
{
    Console.WriteLine(outworker.FIO,outworker.Dolj,outworker.Staj,outworker.Zp);
}
Ravi Patel
  • 451
  • 1
  • 6
  • 17