I have a var allList which contains a list of all the accounts in my tables , I want to order the account ids based on the parent number which the first three digits or four digits, if string have similar starting digits can be next to that,
I have the following data as the output per now
101
202
303
404
10111
10122
20211
20222
303211
101112
101222
10111221
42215
10111223
3035422525
1011122121
I want the output to be like this
101
10111
10122
101112
101222
10111221
10111223
1011122121
202
20211
2022222
2023221
303
303211
3035422525
404
42215
I have tried this code, the accountid is string,can we use thenby or trimstrat
allList = allList.OrderBy(x => x.accountId);
It seems that you want something like this:
allList = allList
.OrderBy(item => item.Length <= 3 ? item : item.Substring(0, 3))
.ThenBy(item => item.Length)
.ThenBy(item => item)
.ToList();
Here we sort
By first 3 digits ("first three digits or four digits"): 101 < 404
On tie, by Length: 10122 < 101112
On tie, by items lexicographically: 10111221 < 10111223
Related
We want get crc code from a string.
for example: string is (ff03c1) and crc code is (3d).
The bellow code works correctly until the string is less than 186 characters.
sample string:
20000F38080000D1080020110800190D0000000000000000000000000000000020000F38080000D1080020110800190D000000000000000000000000000000020000F38080000D1080020110800190D000000000000000000000000000
But this string not working (187 characters):
20000F38080000D1080020110800190D0000000000000000000000000000000020000F38080000D1080020110800190D000000000000000000000000000000020000F38080000D1080020110800190D000000000000000000000000000**0**
error:
Index and length must refer to a location within the string.
Parameter name: length
public static string CreateCRCCode(string Value)
{
return Enumerable.Range(0, Value.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToInt32(Value.Substring(x, 2), 16))
.Aggregate((i, i1) => i ^ i1)
.ToString("X");
}
how we can use string more than 186 character?
Root cause
The real problem is not with the 186 or 187 characters, the problem is the odd and even, what I tried to say is, you will get the same error for an input of 200 as well. The reason is that,
Consider that the Value = "200" so, Value.Length = 3 and hence Enumerable.Range(0, Value.Length) will gives you 0,1,2.
After applying .Where(x => x % 2 == 0) the collection became 0,2.
So when applying the substring(Value.Substring(x, 2)) it will search for the substring starts at index 2 and off length 2(in the second iteration) which is not a valid index. That causes the error.
Proposed Fix:
I don't get any reason for applying Where(x => x % 2 == 0) in the given snippet, if it is necessary please cross check the conditions and scenarios.
Change the Enumerable.Range based on the collection length like the following:
Enumerable.Range(0, Value.Length % 2 == 0 ? Value.Length : Value.Length-1)
I have a list.
1 2 3 4 5 6 7
I wish to return a list of differences (deltas) between consecutive element.
1 1 1 1 1 1
How can I do this?
I am sure there must be a simple "collections" way of doing this - but I cannot find it.
You can use Enumerable.Skip and the overload of Enumerable.Select which projects the index:
List<int> deltaList = list.Skip(1) // skip first, irrelevant
.Select((num, index) => num - list[index]) // index 0 is second number in list
.ToList();
The trick is that Skip(1) does not only skip the first number (which is desired) but also changes the indices in Select. The first number's index will be 0 but it'll refer to the second number in the list (due to Skip(1)). Therefore num - list[index] subtracts the current with the previous number.
var result = list.Zip(list.Skip(1), (x, y) => y - x);
I'm trying to get a list of string ordered such that the longest are on either end of the list and the shortest are in the middle. For example:
A
BB
CCC
DDDD
EEEEE
FFFFFF
would get sorted as:
FFFFFF
DDDD
BB
A
CCC
EEEEE
EDIT: To clarify, I was specifically looking for a LINQ implementation to achieve the desired results because I wasn't sure how/if it was possible to do using LINQ.
You could create two ordered groups, then order the first group descending(already done) and the second group ascending:
var strings = new List<string> {
"A",
"BB",
"CCC",
"DDDD",
"EEEEE",
"FFFFFF"};
var two = strings.OrderByDescending(str => str.Length)
.Select((str, index) => new { str, index })
.GroupBy(x => x.index % 2)
.ToList(); // two groups, ToList to prevent double execution in following query
List<string> ordered = two.First()
.Concat(two.Last().OrderBy(x => x.str.Length))
.Select(x => x.str)
.ToList();
Result:
[0] "FFFFFF" string
[1] "DDDD" string
[2] "BB" string
[3] "A" string
[4] "CCC" string
[5] "EEEEE" string
Don't ask how and why... ^^
list.Sort(); // In case the list is not already sorted.
var length = list.Count;
var result = Enumerable.Range(0, length)
.Select(i => length - 1 - 2 * i)
.Select(i => list[Math.Abs(i - (i >> 31))])
.ToList();
Okay, before I forget how it works, here you go.
A list with 6 items for example has to be reordered to this; the longest string is at index 5, the shortest one at index 0 of the presorted list.
5 3 1 0 2 4
We start with Enumerable.Range(0, length) yielding
0 1 2 3 4 5
then we apply i => length - 1 - 2 * i yielding
5 3 1 -1 -3 -5
and we have the non-negative part correct. Now note that i >> 31 is an arithmetic left shift and will copy the sign bit into all bits. Therefore non-negative numbers yield 0 while negative numbers yield -1. That in turn means subtracting i >> 31 will not change non-negative numbers but add 1 to negative numbers yielding
5 3 1 0 -2 -4
and now we finally apply Math.Abs() and get
5 3 1 0 2 4
which is the desired result. It works similarly for lists of odd length.
Just another option, which I find more readable and easy to follow:
You have an ordered list:
var strings = new List<string> {
"A",
"BB",
"CCC",
"DDDD",
"EEEEE",
"FFFFFF"};
Create a new list and simply alternate where you add items::
var new_list = new List<string>(); // This will hold your results
bool start = true; // Insert at head or tail
foreach (var s in strings)
{
if (start)
new_list.Insert(0,s);
else
new_list.Add(s);
start = !start; // Flip the insert location
}
Sweet and simple :)
As for Daniel Bruckner comment, if you care about which strings comes first, you could also change the start condition to:
// This will make sure the longest strings is first
bool start= strings.Count()%2 == 1;
This is the code:
var numbers =
lightningsRegions.SelectMany(
s => Regex.Matches(s, #"\[(\d+)[ -]+(\d+)\]")
.Cast<Match>()
.Select(m => m.Groups.Cast<Group>().Skip(1).Select(x => x.Value)
.ToArray())
.Select(x => new { start = int.Parse(x[0]), end = int.Parse(x[1]) })
.SelectMany(x => Enumerable.Range(x.start, x.end - x.start + 1))
)
.ToList();
for (int i = 0; i < list_of_histogramsR.Count ; i++)
{
if (list_of_histogramsR[i] == numbers[i])
{
}
}
I consider the variable numbers as number of indexs. In the end numbers contain 5372 numbers.
So each number from thr 5272 is like an index.
Now i have this List<long[]> list_of_histogramsR wich contain 16595 indexs.
I want to check that if any number from numbers is in list_of_histogramsR as index number then do something.
For example the first number in numbers is 41. So when index number 41 of list_of_histogramsR == to the number 41 in numbers do something. Then the same for the next numbers in the variable numbers.
The problem is that on the IF line im getting error: Error 33 Operator '==' cannot be applied to operands of type 'long[]' and 'int'
Why ?
You can use Contains to check if the list contains a specific number (cast the int to a long):
list_of_histogramsR[i].Contains((long)numbers[i])
I have the following c# code that sorts a string in a lexicographical (alphabetical) order.
string str = "ACGGACGAACT";
IEnumerable<string> sortedSubstrings =
Enumerable.Range(0, str.Length)
.Select(i => str.Substring(i))
.OrderBy(s => s);
Result:
0 AACT
1 ACGAACT
2 ACGGACGAACT
3 ACT
4 CGAACT
5 CGGACGAACT
6 CT
7 GAACT
8 GACGAACT
9 GACGAACT
10 T
However I want to enhance this sort by skipping the 3rd and the 4th character during the lexicographical sort process
In this case the lexicographical sort will be different to the one above.
result:
0 AA[CT
1 AC[T
2 AC[GG]ACGAACT
3 AC[GA]ACT
4 CG[GA]CGAACT
5 CG[AA]CT
6 CT
7 GA[CG]AACT
8 GA[AC]T
9 GG[AC]GAACT
10 T
how can I achieve this?
This can be done by tweaking the lambda passed to OrderBy. Something like this should do it:
var sortedSubstrings =
Enumerable.Range(0, str.Length)
.Select(i => str.Substring(i))
.OrderBy(s => s.Length < 3 ? s : s.Remove(2, Math.Min(s.Length - 2, 2)));
Edit: Corrected off-by-one error.
You can change the lambda passed to OrderBy to one which will remove the 3rd and 4th symbols from the string.
var sorted = source.OrderBy(s => new string(s.Where((ch, n) => n != 2 && n != 3).ToArray()));