Home > OS >  c# Sorted List in Function
c# Sorted List in Function

Time:11-21

hi i want to make a sorted list in ShowSort function it shows randomly i want to sort numbers from smallest to largest pls help

private static void hatekleme(List<Hat> hatlar)
 {
   List<Islem> islem1 = new List<Islem>();
   islem1.Add(new Islem { Id = 1, Name = "tv ekranı", Time = 0, Time1 = 0, Time2 = 0, Time3 = 0 });
   islem1.Add(new Islem { Id = 2, Name = "tv kasası ", Time = 0, Time1 = 0, Time2 = 0, Time3 = 0 });
   islem1.Add(new Islem { Id = 3, Name = "tv vidas", Time = 0, Time1 = 0, Time2 = 0, Time3 = 0 });

   List<Islem> islem2 = new List<Islem>();
   islem2.Add(new Islem { Id = 1, Name = "işlemci  ", Time = 0, Time1 = 0, Time2 = 0, Time3 = 0 });
   islem2.Add(new Islem { Id = 2, Name = "ram", Time = 0, Time1 = 0, Time2 = 0, Time3 = 0 });
   islem2.Add(new Islem { Id = 3, Name = "gpu", Time = 0, Time1 = 0, Time2 = 0, Time3 = 0 });

   hatlar.Add(new Hat { Id = 1, Name = "Tv Üretim Hattı", Islemler = islem1 });
   hatlar.Add(new Hat { Id = 2, Name = "Anakart üretim hattı", Islemler = islem2 });

 }

private static void ShowSort(Hat category)
 {
   foreach (var product in category.Islemler)
    {
    Console.WriteLine($"{product.Name} : {product.Time} sn.");
    }
     Console.WriteLine("");
  }

CodePudding user response:

You can easily sort items in a list by OrderBy:

private static void ShowSort(Hat category)
        {
            var sortedItems=category.Islemler.OrderBy(x => x.Time);
            foreach (var product in sortedItems)
            {
                Console.WriteLine($"{product.Name} : {product.Time} sn.");
            }
            Console.WriteLine("");
        }
  • Related