This question already has answers here:
How to Sort a List<T> by a property in the object
(23 answers)
Closed 6 years ago.
i got an output from a list into an excel file. To simplefile things it could look like this:
4
1
2
3
Now all i wanna do is, put it in the right order:
1
2
3
4
which i did with this code ( isSort contains 4 1 2 3 ):
...
var isSortFin = new List<Item>();
var FirstElement = isSort.First();
foreach (var Itemd in toSort)
{
if (Itemd.Summary != FirstElement.Summary)
{
isSortFin.Add(Itemd);
}
}
isSortFin.Add(FirstElement);
return isSortFin;
now my output is:
3 2 1 4
and not the aspired:
1 2 3 4
where do i go wrong?
You could use OrderBy linq extension and sort element.
isSort= isSort.OrderBy(x=>x).ToList();
If Item is an object, specify property name in OrderBy expression to sort on that property.
//ex..
isSort= isSort.OrderBy(x=>x.Value).ToList();
Related
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_
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());
This question already has answers here:
Is the list order of a ConcurrentDictionary guaranteed?
(2 answers)
Closed 4 years ago.
I add four elements in a Dictionary, and iterate trough the dictionary with a ForEach loop : items are in the order of adding.
I add four elements in a ConcurentDictionary, and iterate trough the ConcurentDictionnary with a ForEach loop : items are NOT in the order of adding.
Why ?
static void Main(string[] args)
{
Console.WriteLine("Add four items to dictionnary");
var d = new Dictionary<decimal, int>();
d.Add(1.05m, 1);
d.Add(2.3m, 2);
d.Add(1.3m, 3);
d.Add(4m, 4);
Console.WriteLine("iterate trough Dictionnary");
foreach (var x in d) Console.WriteLine($"{x.Key:0.000} {x.Value}");
Console.WriteLine("\r\nAdd four items to ConcurrentDictionary");
var cd = new ConcurrentDictionary<decimal,int>();
cd.TryAdd(1.05m, 1);
cd.TryAdd(2.3m, 2);
cd.TryAdd(1.3m, 3);
cd.TryAdd(4m, 4);
Console.WriteLine("iterate trough ConcurrentDictionary");
foreach (var x in cd) Console.WriteLine($"{x.Key:0.000} {x.Value}");
Console.WriteLine("\r\niterate trough Keys");
foreach (var x in cd.Keys) Console.WriteLine($"{x:0.000}");
Console.WriteLine("\r\niterate trough Keys");
foreach (var x in cd.Values) Console.WriteLine($"{x}");
Console.ReadKey();
}
Result
Add four items to dictionnary
iterate trough Dictionnary
1.050 1
2.300 2
1.300 3
4.000 4
Add four items to ConcurrentDictionary
iterate trough ConcurrentDictionary
4.000 4
2.300 2
1.300 3
1.050 1
iterate trough Keys
4.000
2.300
1.300
1.050
iterate trough Values
4
2
3
1
edit : the test shows that even if the order is not guaranteed in the documentation, in fact it is.
I read the source in ConcurrentDictionary.cs, and I found why ConcurrentDictionary is different from Dictionary.
I try to write a response ( am french and beginner to write a question about stackoverflow)
The MSDN documentation for a Dictionary (https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2) states that
The order in which the items are returned is undefined
Although the documentation for a ConcurrentDictionary doesn't say so (https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentdictionary-2), the same is true for this too. This question
Is the list order of a ConcurrentDictionary guaranteed? also states this to be the case.
If you want a sorted dictionary, there is a SortedDictionary class, but it is not Concurrent - if you want this, you'll have to write your own, or use a different approach for your code.
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();
This question already has answers here:
How to replace list item in best way
(12 answers)
Closed 5 years ago.
How do I replace a value in a collection list at the same location?
0 = cat
1 = dog
2 = bird
replace 2 with snail?
Do you mean:
yourCollection[2] = "Snail";
In a bigger List<T> collection, you would like to find index to replace...with:
var i = animals.FindIndex(x => x == "Dog");
animals[i] = "Snail";
List<string> animals = new List<string>();
animals.Add("cat");
animals.Add("dog");
animals.Add("bird");
animals.Add("fish");
animals.RemoveAt(2);
animals.Insert(2, "snail");