Combine to array sorts into one? [duplicate] - c#

This question already has answers here:
How can I sort a List<T> by multiple T.attributes?
(3 answers)
Closed 4 years ago.
I was wondering if it is possible to combine my sorting code into one ?
I sort my array firstly by enum, then by an id number from lowest to highest.
So my current code looks like this:
_entities = _entities.OrderBy(a => a.Priority).ToArray(); //sort by enum
_entities = _entities.OrderBy(a => a.PriorityID).ToArray(); //then sort by id
So the enum has:
High, Medium,Low
And IDs are just integers.
So the end result should be like:
High¬
0 , 1 ,2
Medium¬
0, 1, 2
Low¬
0, 1, 2
I don't know the syntax to combine these, or if this is as simple as it gets? But it does seem a bit inefficient to be sorting by each property one by one.
Whats the correct way to do ths?

You can use ThenBy:
_entities = _entities.OrderBy(a => a.Priority).ThenBy(a => a.PriorityID).ToArray();

Related

Get part of the list using LINQ [duplicate]

This question already has answers here:
Subset of Array in C#
(9 answers)
Closed 1 year ago.
Say, I have a list and I want to get a portion of it. Say I have 1000 data from 0 to 999. Then I want to get from "index1" to "index2."
Sample data is :
[0] = 100
[1] = 1520
....
[900] = 8975
....
[998] = 10
[999] = 4875
Say for example I want to get values from index 900 to index 998. So the value return should be:
[0] = 8975
.....
[998] = 10
How to do that in LINQ?
You can use skip and take for that
List<int> list= new List<int>;
list.Skip(900).Take(100);
https://www.codingame.com/playgrounds/213/using-c-linq---a-practical-overview/skip-and-take
Or, you can use the GetRange method
List<int> list= new List<int>;
list.GetRange(900, 100); // Retrieves 100 items starting with index #900
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.getrange?redirectedfrom=MSDN&view=net-6.0#System_Collections_Generic_List_1_GetRange_System_Int32_System_Int32_

C# Sort List according to another List values [duplicate]

This question already has answers here:
C# Sort List Based on Another List
(7 answers)
C# Sort list by IDs, but without creating a new list
(2 answers)
Sorting two separate Lists in the same order?
(4 answers)
Sort two Lists<T> together as one?
(7 answers)
Identically sorting two lists
(4 answers)
Closed 1 year ago.
I need to sort one list (string) according to another list values (int) from the smallest to the highest int.
Possibility to have several times the same string.
For example:
List<string> list1= new List<string>() { "abc", "xyzz", "abc", "fgh", "abc" };
List<int> list2 = new List<int>() { 8, 15, 4, 2, 22 };
output:
list1 {"fgh","abc", "abc", "xyzz", "abc"}
This line should probably work but I still miss something:
list1 = list1.OrderBy(d => list2."i miss something here".ToList());

Swap two lists in c# [duplicate]

This question already has answers here:
Rotate - Transposing a List<List<string>> using LINQ C#
(7 answers)
Closed 5 years ago.
I have a List of a List (a matrix, which is sent via web.api HTTP POST from matlab).
List<List<decimal>> mylist;
The size of the matrix is nxm, how can I swap those lists? i.e.
mylist[i][j] --> mylist[j][i]
In Matlab the operation would be mylist' or in mathematical context (transposing)
mylist^T
You could use Linq to achieve that without for loop like this:
var swapedList =
mylist
.SelectMany((l, i) => l.Select((d, j) => new { i, j, d }))
.GroupBy(l=>l.j)
.Select(l=>l.Select(ll=>ll.d).ToList());
.ToList();
I hope to be helpful for you :)

C# Getting a specific position in a List [duplicate]

This question already has answers here:
c# list<int> how to insert a new value in between two values
(5 answers)
Closed 5 years ago.
Im seeking for a tiny help with a List I have
So, I have a List, which can change at any time, its quite long so I wont post it,
I want to get a specific position because I want to insert a new Line into it outside of its source. Here is a small thing that might explain it
Before
public class ListSourceView
{
public List<Stuff> Blah = new List<Stuff>("Hello", "Principal", "Johnson,", "How are you?");
}
So, we have a List called Blah with 4 things in it, now I would like to put a new String before Principal, but regardless if there are more strings in the list. (Aka Blah.Insert(2, "Mr") would not work) Best Case scenario would be something like Blah.Insert(Principal.positionInTheList - 1, "Mr") but am unsure what to use here.
Hope ya help a buddy out
You can use:
int index = Blah.FindIndex(a => a.StartsWith("Principal"));
then you can insert a new item at/before/after the index by using
Blah.Insert(index, "newEntry"); //at index
Blah.Insert(index - 1, "newEntry"); //before index
Blah.Insert(index + 1, "newEntry"); //after index

How to get the numeric value from a flags enum? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Enums returning int value
How to get the numeric value from the Enum?
THIS IS NOT A DUPLICATE OF EITHER OF THESE, THANKS FOR READING THE QUESTION MODS
Suppose I have a number of my flags enum items selected:
[Flags]
public enum Options
{
None = 0,
Option_A = 1,
Option_B = 2,
Option_C = 4,
Option_D = 8,
}
Options selected = Options.Option_A | Options.Option_B;
The value of selected should correspond to 3 (i.e. 2 + 1)
How can I get this into an int?
I've seen examples where the selected is cast ToString() and then split() into each option, e.g.
"Option_A | Option_B" --> { "Option_A", "Option_B" },
then reconstituted into the respective Enum, and the values taken from that, but it's a bit messy. Is there a more straight-forward way to get the sum of these values?
There is not much options as just make an if
List<int> composedValues = new ....
if((selected & Option_A) == Options.Option_A)
composedValues.Add((int)Options.Option_A);
else if((selected & Option_B) == Options.Option_B)
composedValues.Add((int)Options.Option_B);
else if(...)
Finally you will get a list of all compositional values of the result in the composedValues list.
If this is not what you're asking for, please clarify.

Categories

Resources