-4

Please note to the following topic that has already been raised :

How to Sort a List by a property in the object

I want to sort a list by another list. I add to the list with the following command :

ListClass.Add(new item_util(items[b], util[b]));

Now based on the above topic , I can sort items by ascending :

List<item_util> SortedList = ListClass.OrderBy(o=>o.items).ToList();

I do not want to sort ascending. How can I sort items by another list (ListA)?

Updated

int[] items = {7, 4, 1};
int[] util  = {5, 11, 20};
var listA   = new List<int> {5, 4, 1, 2, 7, 3, 8, 6};

List<item_util> ListClass= new List<item_util>();
for (int b = 0; b < items.Length; b++)
    ListClass.Add(new item_util(items[b], util[b]));
List<item_util> SortedList = ListClass.OrderBy(o => o.item).ToList();

Currently SortedList output is equal to: {(1,20),(4,11),(7,5)}

But I want to sort the list acording to listA. So the output must be obtained as : {(4, 11), (1, 20), (7, 5)}

Because they are arranged in ListA as : {5, 4, 1, 2, 7, 3, 8, 6}

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Reza Hatami
  • 102
  • 6

2 Answers2

0

To handle large lists, and duplicates and missing values in listA, you need to do something like this:

var indices = listA.Select((x, n) => new { x, n }).ToLookup(y => y.x, y => y.n);

List<item_util> SortedList =
    ListClass
        .OrderBy(o => indices[o.item].Append(int.MaxValue).First())
        .ToList();

If listA becomes large then using Array.IndexOf will slow the code down immensely as it would be a O(n^2) operation. The .ToLookup generates an O(n) operation as far as the sort is concerned.

Also, if items are missing from listA the use of .Append(int.MaxValue) will push those values to the end of the sort.


If .Append is not available it must be because you're on an earlier version of the framework. It's built-in to mine.

Try this instead:

List<item_util> SortedList =
    ListClass
        .OrderBy(o => indices[o.item].Concat(new [] { int.MaxValue }).First())
        .ToList();
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • ListA is unique and the listA is a big list. recived a error : `'IEnumerable' does not contain a definition for 'Append' and no extension method 'Append' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?)` – Reza Hatami Jun 06 '18 at 11:42
  • @Amin - I've amended my answer for earlier versions of the framework, – Enigmativity Jun 06 '18 at 12:40
-3

Your code doesn't make too much sense to me, but this will give you the idea and generates the output you are looking for:

var listA = new int [] {5,4,1,2,7,3,8,6};
var listB = new List<ListClass>
{
    new ListClass(1, 20),
    new ListClass(4, 11),
    new ListClass(7, 5)
};
var results = listB.OrderBy(p => Array.IndexOf(listA, p.X));

foreach (var r in results) Console.WriteLine("{0}, {1}", r.X, r.Y);

Result:

4, 11
1, 20
7, 5

Click this link to see the example on DotNetFiddle

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
John Wu
  • 50,556
  • 8
  • 44
  • 80