Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving a list of ints to the front of a list

I have a List of Int64 (List A) that needs to moved infront of another List (Int64) (List B).

List B will ALWAYS contain the numbers from List A.

So say the List A has the following numbers:

1, 4, 5

List B could then look something like this:

1, 9, 5, 2, 10, 15, 4

The end result should then look like this:

1, 4, 5, 9, 2, 10, 15

What is the easiest way of moving the numbers from the first list to the front of the second list?

I thought about removing all List A numbers from List B and then adding them to front again, but I can't seem to grasp my head around the programming itself.

like image 950
Dumpen Avatar asked Feb 17 '23 18:02

Dumpen


1 Answers

Yo can try the following:

var result = listA.Concat(listB.Except(listA)).ToList();
// Gives: 1, 4, 5, 9, 2, 10, 15, 14

Except removes all elements of listA from listB. Concat then adds them to the front of the list.

like image 177
Wouter de Kort Avatar answered Feb 26 '23 18:02

Wouter de Kort