Solved

Sort a generic List<Article> for its specific property

Posted on 2009-07-02
14
197 Views
Last Modified: 2012-05-07
Hello,

I have this class object

class Article
{    
        public string ArticleDescription { get; set; }
        public string WG { get; set; }
        public string Archived{ get; set; }
}

Now I have put many article objects into a

var articleList = new List();

articleList.Add(new Article());

the articleList has many objects now... How can I sort for the Property = ArticleDescription for example?
0
Comment
Question by:MSFanboy
[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
  • 8
  • 5
14 Comments
 
LVL 9

Assisted Solution

by:Rahul Goel ITIL
Rahul Goel ITIL earned 150 total points
ID: 24761964
0
 
LVL 39

Expert Comment

by:abel
ID: 24761977
I copied your code and used the following (.NET 3.5) to sort it by WG. You can do the same with ArticleDescription. The code below prints the following

Unsorted:
balena
chris
anton

Sorted:
anton
balena
chris

-- Abel --

IList<Article> articleList = new List<Article>();
articleList.Add(new Article() { ArticleDescription = "other descr", WG = "balena" });
articleList.Add(new Article() { ArticleDescription = "yet other descr", WG = "chris" });
articleList.Add(new Article() { ArticleDescription = "description", WG = "anton" });
Debug.WriteLine("Unsorted:");
foreach (var a in articleList)
    Debug.WriteLine(a.WG);
 
articleList = articleList.OrderBy(article => article.WG).ToList();
Debug.WriteLine("\nSorted:");
foreach (var a in articleList)
    Debug.WriteLine(a.WG);

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 24762008
In other words, using LINQ (.OrderBy(), you may need to add a reference to System.Core to your project and an "using System.Linq" to the top of your class) makes this real easy.

- Before the "=>" symbol goes the name of the variable you want to sort on. Choose any, its type is the List type
- After the "=>" symbol goes the property you want to sort on. Use "article.ArticleDescription" in your case

What's important in my example is that I used the Generic List. You said so in your question, but you didn't show so in your code. Which is why I added the declaration of the generic list at the beginning of the code.

-- Abel --
0
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.

 

Author Comment

by:MSFanboy
ID: 24762511
How can I sort several properties without using linq?
0
 

Author Comment

by:MSFanboy
ID: 24762586
or with linq is it possible at all to make orderby for several properties?

lets say I have 4 properties in a class:

1. articlenumber
2. articlename
3. articledate
4. category

I want first sort after articlenumber then articlename then articledate and and last category

Would be cool if someone can help me still have 3 hours to do this :/ damn should have learned before linq :P
0
 
LVL 39

Expert Comment

by:abel
ID: 24762813
complex task (relatively), only 3 hours before deadline (???) and only a voluntary service like EE to help you? That's scary...

You go quite a bit further then your original question. But I'll have a look, can't promise that I stay within your time constraints though, I have a job too... ;-)
0
 
LVL 39

Expert Comment

by:abel
ID: 24762862
Luckily, the answer is very easy. Simply chain your OrderBy clauses (each OrderBy returns an IEnumerable anyway):

articleList = articleList
    .OrderBy(article => article.ArticleNumer)
    .OrderBy(article => article.ArticleName)
    .OrderBy(article => article.ArticleDate)
    .OrderBy(article => article.ArticleCategory).ToList();

Open in new window

0
 
LVL 39

Expert Comment

by:abel
ID: 24762876
oops, forget that. That's not true, of course. I'm being to quick. Erase that from your memory... ;-)
0
 
LVL 39

Accepted Solution

by:
abel earned 350 total points
ID: 24762936
So, back on track, this time I tested first. This is LINQ and LINQ is easy, but you have to know how to. Here's two ways:

// using method calls
articleList = articleList
    .OrderBy(article => article.ArticleNumber)
    .ThenBy(article => article.ArticleName)
    .ThenBy(article => article.ArticleDate)
    .ThenBy(article => article.ArticleCategory).ToList();
 
// using LINQ syntax
articleList = (from art in articleList
              orderby art.ArticleNumber, 
                      art.ArticleName, 
                      art.ArticleDate, 
                      art.ArticleCategory
              select art).ToList();

Open in new window

0
 

Author Comment

by:MSFanboy
ID: 24762955
hey I got the same result as you but I choosed this:

instead of using the last 3 OrderBy I used "ThenBy" the rest is the same with your code :)

lets assume I have this output (I did not sort it here now... just example...)

quantity ... category
10.............stuff1
12.............stuff1
33.............stuff5
5 .............stuff5
12.............stuff7
33 .............stuff1          
55.............stuff3
10.............stuff3
3.............stuff7

How can I sum the quantity of every category?

so I have in the end thiat output:

55 stuff1
38 stuff5
14 stuff7
65 stuff3

?

0
 
LVL 39

Expert Comment

by:abel
ID: 24762995
> How can I sum the quantity of every category?

you are drifting further and further away from your original question. At EE, it is recommended (and actually mandatory) to stick to one question per thread: http://www.experts-exchange.com/help.jsp?hi=23

You have unlimited points. Consider asking these specific questions inside the LINQ zone. You'll probably get more to the point answers there from the LINQ experts.
0
 

Author Comment

by:MSFanboy
ID: 24763070
yeah sorry my boss was just here changing requirements arghhh stupid... i go to linq and close here later, thx a bunch guys!
0
 

Author Closing Comment

by:MSFanboy
ID: 31599116
@Abel you get 350 points for posting much and I use your linq solution as I use .net 3.5

@Rahu_ketu_patal: you get 150 points as your link is very helpfull where I am limited to .net 2.0 projects only. Your link shows it with .net 3.0 but its  also possible with 2.0 just less code to write.
0
 
LVL 39

Expert Comment

by:abel
ID: 24771520
After we stuck with you through all additional question and explaining all the lot, giving you cut-n-paste examples, why the B-grade? You asked more then the original q. and got more answers, shouldn't you grade accordingly?

Grading experts is not like at school, it is more a 10 points must system: http://www.experts-exchange.com/help.jsp?hi=403

Changing grades: http://www.experts-exchange.com/help.jsp?hi=404

-- Abel --
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

707 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