Hirdetés

Új hozzászólás Aktív témák

  • Peter Kiss

    őstag

    Másik lehetőség:

    using System;
    using System.Collections.Generic;

    namespace ConsoleApplication1
    {
    internal class Model
    {
    public int A { get; set; }
    public int B { get; set; }
    public int C { get; set; }
    }

    class Program
    {
    static void Main(string[] args)
    {
    var sorted = new SortedList<int, Model>
    {
    {1, new Model {A = 1, B = 5, C = 7}},
    {3, new Model {A = 3, B = 7, C = 9}},
    {2, new Model {A = 2, B = 5, C = 4}},
    {5, new Model {A = 5, B = 3, C = 1}}
    };

    foreach (var item in sorted.Values)
    {
    Console.WriteLine("{0} {1} {2}", item.A, item.B, item.C);
    }

    Console.ReadLine();
    }
    }
    }

    Ha nem simétlődik az A adat:
    using System;
    using System.Collections.Generic;

    namespace ConsoleApplication1
    {
    internal class Model
    {
    public int A { get; set; }
    public int B { get; set; }
    public int C { get; set; }
    }

    class ModelComparer : IComparer<Model>
    {
    public int Compare(Model x, Model y)
    {
    if (x.A > y.A)
    {
    return 1;
    }

    return x.A == y.A ? 0 : -1;
    }
    }

    class Program
    {
    static void Main(string[] args)
    {
    var sortedSet = new SortedSet<Model>(new ModelComparer())
    {
    new Model {A = 1, B = 5, C = 7},
    new Model {A = 1, B = 5, C = 7},
    new Model {A = 3, B = 7, C = 9},
    new Model {A = 2, B = 5, C = 4},
    new Model {A = 5, B = 3, C = 1}
    };
    foreach (var item in sortedSet)
    {
    Console.WriteLine("{0} {1} {2}", item.A, item.B, item.C);
    }

    Console.ReadLine();
    }
    }
    }

Új hozzászólás Aktív témák