In C #, what is the best way to sort a list of objects using the string property and get the correct order?

I have a list of Problem objects, and I want to sort them by the Priority field.

The problem is that “Priority” is a string name like “HIGH”, “MEDIUM”, so I don’t have an identifier that I can sort. How to sort and tell the sorter that "HIGH" is higher than "MEDIUM" which is above "LOW"?

+1
source share
5 answers

The obvious will be:

string[] priorities = { "LOW", "MEDIUM", "HIGH" }; var orderedIssues = issues.OrderByDescending (issue => Array.IndexOf(priorities, issue.Priority)); 

But consider using an enumeration:

 public enum Priority { Low, Medium, High } var orderedIssues = issues.OrderByDescending (issue => (Priority)Enum.Parse(typeof(Priority), issue.Priority, true)); 

It is even better to use the enumeration type as the type of the property / field itself, in which case it is as simple (and less error prone) as:

 var orderedIssues = issues.OrderByDescending(issue => issue.Priority); 
+10
source

The easiest way will probably look like this:

 private static int MapPriority(string priority) { switch(priority.ToUpperInvariant())//skip the case bit if safe { case "HIGH": return 1; case "MEDIUM": return 2; case "LOW": return 3; default: return 4; } } var sorted = someCollection.OrderBy(i => MapPriority(i.PriorityProperty)); 

With the form supported by db, you will need a function in the database that you can call. It is only in memory.

With a lot of possible meanings, I would base it on a dictionary, not a manual code. I would make a manual codeword for three, as in this case, though (unless the values ​​used can change, a further complication makes the dictionary-based approach the only way).

If you sort a large number of such elements or call them many, I would go with the implementation of IComparer<T> or the element itself would implement IComparable<T> .

+2
source

In this particular case, you can also use the Linq OrderBy method:

 var sortedList = issueList.OrderBy(i=> i.Priority == "HIGH" ? 1 : i.Priority == "MEDIUM" ? 2 : 3).ToList(); 

As a single line, that would not be so bad. You can also put strings in an array, list, or dictionary in the order in which you want to sort them (or contain the sort order as a value in the case of a dictionary).

The only drawback to using OrderBy is that it does not affect the original list unless you tell it by reassigning the list to the result. In all cases, he will create two additional collections; an internally used array or list in OrderBy (sortings must know all sorting they sort) and List created by ToList (). Thus, this will require O (2N) additional memory, while List.Sort () may be in place (not sure if this is true, but it uses QuickSort, which is usually in place).

+2
source
 public enum Priority { LOW = 1, MEDIUM = 2, HIGH = 3 } issues.OrderByDescending(issue=>issue.Priority); 
+1
source

Something like that:

 List<Issue> issues = ...; var result = issues.OrderBy(x=> x.Priority=="HIGH"?1:x.Priority=="MEDIUM"?2:3); 
0
source

Source: https://habr.com/ru/post/1264932/


All Articles