Sort list without creating a new variable

I am trying to use Enumerable.OrderBy to sort a list, because ultimately I want to be able to sort more than one field. At the moment, it only works if I create a new var variable to hold the presentation of the results, which means ( I think) that types should be re-mapped.

Is there a way to sort the list by more than 1 field while keeping the original List variable and types? That is, I would rather get a variable _orderedbins type List<orderedbins>

Below is what I currently have, but everything from var test = ... further seems a bit wrong.

 public class orderedBins { public string Bin { get; set; } public int Order { get; set; } } List<orderedbins> _orderedbins = new List<orderedbins>(); foreach (string item in splitbins) { string[] spbinSetting = item.Split(','); bool bchecked = bool.Parse(spbinSetting[1]); int border = int.Parse(spbinSetting[2]); if (bchecked == true) { _orderedbins.Add(new orderedbins { bin = spbinSetting[0], Order = border }); } } var test =_orderedbins.OrderBy(x => x.Order); foreach (var item in test) { string f = item.Bin; int g = item.Order; } 
+4
source share
5 answers

You know, you can perform several subtypes of order with ...

 lst.OrderBy(x => x.Prop1).ThenBy(x => x.Prop2).ThenByDescending(x => x.Prop3)... 

Just add .ToList(); and enter it with a variable to get the result in a list variable.

EDIT:

Willem's great offer for greater readability:

 from x in lst order by x.Prop1, x.Prop2, x.Prop3 select x 
+5
source

This will do the trick:

 _orderedbins = _orderedbins.OrderBy(x => x.Order).ToList(); 

... but there is no real problem when creating a new variable / reference.

+3
source

You can create a new sorted list without creating a new variable using

 list = list.OrderBy(item => item.Field1).ThenBy(item => item.Field1).ToList(); 

It will still create an entirely new list (although in fact it is not a problem to add a new variable, it is cheap). Creating a new list by doing this is fine, as long as the list is not very large.

If you need to sort the list in place, you will want to use your own resolver with the list sorting method:

 public class MyComparer : IComparer<MyClass> { public int Compare(MyClass x, MyClass y) { if(x.Field1 != y.Field1) return x.Field1.CompareTo(y.Field1) else return x.Field2.CompareTo(y.Field2); } } List<MyClass> list = new List<MyClass>(); //Populate list list.Sort(new MyComparer()); 
+2
source

As suggested by others, using Linq OrderBy(...).ToList() would be a cleaner way, but it will give you a new copy of the list.

To save the original instance, consider using List<T>.Sort() :

 _orderedbins.Sort(new Comparison<orderedBins>((obj1, obj2) => { int result = obj1.Order.CompareTo(obj2.Order); return result != 0 ? result : obj1.Bin.CompareTo(obj2.Bin); })); 
+2
source

I think this will be done (this is already a list of orders, so casting is not required):

 _orderbins = _orderbins.OrderBy(x => x.Order).ToList(); 
+1
source

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


All Articles