Solved

sort list of objects by object property name

Posted on 2015-01-16
10
205 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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