Solved

sort list of objects by object property name

Posted on 2015-01-16
10
206 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
  • 5
  • 3
  • 2
10 Comments
 
LVL 33

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 32

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 33

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
 
LVL 33

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 62

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 33

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 62

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 33

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 32

Author Closing Comment

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

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

937 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now