Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sort list of objects by object property name

Posted on 2015-01-16
10
Medium Priority
?
231 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 34

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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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 64

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 64

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 34

Author Closing Comment

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

Expert Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

636 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