?
Solved

sort list of objects by object property name

Posted on 2015-01-16
10
Medium Priority
?
224 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 1000 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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.…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

765 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