Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting a generic list by an external sort order

Tags:

c#

linq

I have a generic list

Simplified example

var list = new List<string>()
  {
    "lorem1.doc",
    "lorem2.docx",
    "lorem3.ppt",
    "lorem4.pptx",
    "lorem5.doc",
    "lorem6.doc",
  };

What I would like to do is to sort these items based on an external list ordering

In example

var sortList = new[] { "pptx", "ppt", "docx", "doc" };

// Or
var sortList = new List<string>() { "pptx", "ppt", "docx", "doc" };

Is there anything built-in to linq that could help me achieve this or do I have to go the foreach way?

like image 416
Eric Herlitz Avatar asked Jan 09 '13 11:01

Eric Herlitz


People also ask

Can you sort a list in C#?

Sort() Method Set -1. List<T>. Sort() Method is used to sort the elements or a portion of the elements in the List<T> using either the specified or default IComparer<T> implementation or a provided Comparison<T> delegate to compare list elements.

What is sort in c sharp?

C# is using a default comparer method to sort integers numerically. The Sort method orders the integers in ascending order, while the Reverse method in descending order.

Does C# list maintain order?

The List<> class does guarantee ordering - things will be retained in the list in the order you add them, including duplicates, unless you explicitly sort the list. According to MSDN: ... List "Represents a strongly typed list of objects that can be accessed by index."

How do you sort collections in C#?

Sort(IComparer) This method is used to sort the elements in the entire ArrayList using the specified comparer. This method is an O(n log n) operation, where n is Count; in the worst case, it is an O(n^2) operation. Syntax: public virtual void Sort (IComparer comparer);


3 Answers

With the list you can use IndexOf for Enumerable.OrderBy:

var sorted = list.OrderBy(s => sortList.IndexOf(Path.GetExtension(s)));

So the index of the extension in the sortList determines the priority in the other list. Unknown extensions have highest priority since their index is -1.

But you need to add a dot to the extension to get it working:

var sortList = new List<string>() { ".pptx", ".ppt", ".docx", ".doc" };

If that's not an option you have to fiddle around with Substring or Remove, for example:

var sorted = list.OrderBy(s => sortList.IndexOf(Path.GetExtension(s).Remove(0,1)));
like image 166
Tim Schmelter Avatar answered Nov 18 '22 01:11

Tim Schmelter


This solution will work even if some file names do not have extensions:

var sortList = new List<string>() { "pptx", "ppt", "docx", "doc" };
var list = new List<string>()
  {
    "lorem1.doc",
    "lorem2.docx",
    "lorem3.ppt",
    "lorem4.pptx",
    "lorem5.doc",
    "lorem6.doc",
  };

var result = 
       list.OrderBy(f => sortList.IndexOf(Path.GetExtension(f).Replace(".","")));
like image 34
Sergey Berezovskiy Avatar answered Nov 18 '22 02:11

Sergey Berezovskiy


You could try using Array.IndexOf() method:

var sortedList = list.OrderBy(i => sortList.IndexOf(System.IO.Path.GetExtension(i))).ToList();
like image 32
MarcinJuraszek Avatar answered Nov 18 '22 01:11

MarcinJuraszek