Solved

sort list of objects by object property name

Posted on 2015-01-16
10
218 Views
Last Modified: 2016-02-16
I have a list of objects, defined as

List<CCG_QuantityCheckData> badItems = new List<CCG_QuantityCheckData>();

I'm populating my badItems list like:

badItems.Add(item);

where item is defined as:

foreach (CCG_QuantityCheckData item in CCG_Items)

I want to sort on each objects "balance_on_hand" property, so I tried using LINQ:

badItems = badItems.OrderBy(x >= x.balance_on_hand).ToList();

which yields the message:

The type arguments for method 'System.Linq.Enumerable.OrderBy<TSource,TKey>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,TKey>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.      

what am I doing wrong?
0
Comment
Question by:Big Monty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40554423
Have you tried:

badItems.Sort()

I found this example on the Web:
class Program
{
    static void Main()
    {
	List<string> list = new List<string>();
	list.Add("tuna");
	list.Add("velvetfish");
	list.Add("angler");

	// Sort fish alphabetically, in ascending order (A - Z)
	list.Sort();

	foreach (string value in list)
	{
	    Console.WriteLine(value);
	}
    }
}

Open in new window

0
 
LVL 33

Author Comment

by:Big Monty
ID: 40554479
I looked at Sort() but couldn't figure out how to sort via an object property.

in your example, you just have a list of strings, i need to sort a list of objects
0
 
LVL 34

Accepted Solution

by:
Mike Eghtebas earned 250 total points
ID: 40554495
My bad, try:

List<CCG_QuantityCheckData> SortedObjList = badItems.OrderBy(o=>o.balance_on_hand).ToList();

I am building an example to make sure this works.
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40554590
This example works (I have used the above solution, compare it with line 24 below):
namespace List
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Order> objListOrder = new List<Order>();
            Order ord1 = new Order();
            Order ord2 = new Order();
            Order ord3 = new Order();

            ord1.OrderId = 5;
            ord2.OrderId = 100;
            ord3.OrderId = 33;

            ord1.OrderDate = new DateTime(2014, 1, 1);
            ord2.OrderDate = new DateTime(2013, 3, 1);
            ord3.OrderDate = new DateTime(2012, 2, 1);
             
            objListOrder.Add(ord1);
            objListOrder.Add(ord2);
            objListOrder.Add(ord3);

            List<Order> newList = objListOrder.OrderBy(o => o.OrderId).ToList();

            ShowDates(newList);

        }

        public  static void  ShowDates(List<Order> mylist)
        {
            foreach (Order o in mylist)
            {
                Console.WriteLine(o.OrderId.ToString());
            }

            Console.ReadLine();
        }
         
    }
}

Open in new window

0
 
LVL 63

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
ID: 40554716
Hi Big Monty;

The issue you may be having is that the method OrderBy takes a lambda expression but the operator you have used is a greater then or equal, >=, where it should have been,  =>.

Your code was
badItems = badItems.OrderBy(x >= x.balance_on_hand).ToList();

should be
badItems = badItems.OrderBy(x => x.balance_on_hand).ToList();
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40554759
Fernando,

Thank you for pointing that out. I got to have some free education also.

Mike
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40554761
@Mike, I can't tell you how many times that has happened to me. Well all learn from woking on these post. ;=)
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40554842
Fernando,

One think that makes you very good (but I haven't learned enough on it to be like you yet) is that you read the error message very closely. And, that seems to be your key to success.

Mike
0
 
LVL 33

Author Closing Comment

by:Big Monty
ID: 40555731
thx to both, that was the issue and it's now resolved
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40555843
Not a problem, glad to have been of help.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question