Valerie Meunier

Valerie Meunier

  • 965
  • 693
  • 72.4k

Why are the data not sorted?

Aug 22 2022 12:51 PM

Hi

With this code, i get first Bob, then Bill. So it doesn't sort the data. I found the solution (see below) but i want to know where is the error here, why it doesn't work.

Thanks

V

using System;
using System.Collections.Generic;
using System.Linq; 
class MyClass
{
    public string Naam;
    public int Age; publuc string Country; }

List<MyClass> t = new List<MyClass>();

t.Add(new MyClass() { Name = "Bob", Age = 36, Cuntry = "UK" });

t.Add(new MyClass() { Name = "Bill", Age = 32, Country = "USA" });

.OrderBy(x => x.Age);
        foreach (MyClass x in t)

Console.Write(x.Name + " " + x.Country + ", " + x.Age);

  But with this code, it works:

var sort1 = t.OrderBy(x => x.Age);
        foreach (var x in sort1)
        Console.Write(x.Name + " " + x.Age;

 


Answers (5)