Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# sorting a List<> using Tuple?

Tags:

c#

linq

tuples

I need to sort a List<> of MediaItem objects by publish date...the publish date is not a property of the item. So my initial intention was to temporarily tack on a publish date property, load em up, sort, and ditch the property. Someone at my work suggested I use Tuple and sort with LINQ...I've got about this far in my snippet test, filling up the List<>...but I'm not sure how to sort by that date property using LINQ. I'll keep looking on my end but if anyone's got any help I'd appreciate it. Thanks.

List<Tuple<MediaItem, DateTime>> list = new List<Tuple<MediaItem, DateTime>>();

            for (int i = 0; i <= 10; i++)
            {
                DateTime date = new DateTime(2011, i, 1);
                MediaItem item = new MediaItem();
                list.Add(new Tuple<MediaItem, DateTime>(item, date));
            }
like image 956
J Benjamin Avatar asked Apr 25 '11 17:04

J Benjamin


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

You can use the OrderBy( ) LINQ operator to perform the sorting; it allows you to pass a function which extracts the element to sort by. Since the second member of the tuple is the date, we order by Item2.

var result = list.OrderBy( x => x.Item2 ).ToList();

You can reverse the ordering in LINQ by using OrderByDescending() instead of `OrderBy().

Also, note that you must either materialize the results or iterate over them, as the OrderBy() method is lazy by default. The example above materializes a copy of the list.

If you want to sort the list in place (rather than create a new one), you can supply a Comparison<T> delegate to the Sort() method.

 list.Sort( (a,b) => a.Item2.CompareTo(b.Item2) );

However, you can do even better - if you always want to main the list in sorted order, you can use the SortedList class instead ... however you will have to implement a custom IComparer<Tuple<MediaItem, DateTime>> in that case. In this case, when you add items they will always be maintained in sorted order.

like image 132
LBushkin Avatar answered Sep 28 '22 02:09

LBushkin