0

Apologies if this is a duplicate question, which I think it may be although I cannot find the answer I am looking for.

In my list, I need to sort by the names of the objects in the list. As an example, if in a list of cars I have the objects Volvo, Ford, BMW, Audi, Ferrari and Lamborghini in that order, then I would need it to order the objects Audi then BMW etc.

I have tried doing .OrderBy(x => nameof(x)) but this didn't work. Does anyone have any solutions

Edit: I need to order by the following (other elements hidden because of sensitive data):

screenshot of code

So on Data there is an object called "Aaa" for example which needs to be the first element in this list.

7
  • 3
    nameof(x) returns "x" if I am not mistaken Commented Dec 13, 2016 at 9:50
  • "x" == nameof(x) Commented Dec 13, 2016 at 9:50
  • 1
    What do you mean by "names of the objects"? Does your Car type (or whatever) have a Name property? It's very hard to help without a minimal reproducible example... Commented Dec 13, 2016 at 9:52
  • You should not rely on the names of the variables as they don´t have any meaning for the assembly. However you may consider to add a Name-property to the instances in your list. So you can easily use myList.OrderBy(x => x.Name). Commented Dec 13, 2016 at 9:52
  • The names of the variables are not maintained when the code is compiled, so the names you give your variables are meaningless when it comes to your program's logic. nameof merely cheats a bit to make this less impossible by converting the name into a string literal just before compilation, but it was not intended for this kind of use. Commented Dec 13, 2016 at 9:59

2 Answers 2

1

Instead of relying on the variables names within a collection you should use a Name-property for every instances within that list:

class Car
{
    public string Name { get; set; }
    // ...
}

Now within your consuming code you can easily use this property:

var myList = new List<Car> { 
    new Car { Name = "BMW" },
    new Car { Name = "Audi" }
}:
var result = myList.OrderBy(x => x.Name);

As Abion47 and me already mentioned the names of variables have no meaning to the assembly, just within your sour-code. When you de-compile your assembly you see that the names are replaced by a random dummy-name. So relying on those names is hardly a good idea.

Sign up to request clarification or add additional context in comments.

Comments

0

Below is one of the way using IComparer interface.

//Implementing IComparer

    public  class Cars
    {
       string Name { get; set; }   
    }

    public class SortByName : IComparer<Cars>
    {    
      public int Compare(Cars first, Cars next)
         {
           return first.Name.CompareTo(next.Name);
         }
     }

//Main Method

        static void Main(string[] args)
        {
            Cars one = new Cars { Name = "Ford" };
            Cars two = new Cars { Name = "Audi" };
            Cars three = new Cars { Name = "Lamborghini" };
            Cars four = new Cars { Name = "Ferrari" };

            List<Cars> _lstCars = new List<Cars>();
            _lstCars.Add(one);
            _lstCars.Add(two);
            _lstCars.Add(three);
            _lstCars.Add(four);
            SortByName sortbyname = new SortByName();
            _lstCars.Sort(sortbyname);
            Console.WriteLine("Result After sorting by name");
            foreach (var item in _lstCars)
            {
                Console.WriteLine(item.Name);
                Console.WriteLine();
            }
            Console.Read();
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.