LINQ custom sort using comparer

141 views Asked by At

I have a class:

public class Item
{
  public int ownerID;
  public decimal value;
}

And an array of objects

Item[] items;

That looks like:

ownerID     value
476478      0.65
636566      0.2

And I have a second class

public class Owner
{
  public int ownerID;
  public int index;
}

And an array of objects

Owner[] owners;

The "owners" is sorted in specific order according the index and looks like:

ownerID    index
636566     1
476478     2

How can I sort the "items" in the same order as the "owners" using LINQ and comparer?

Would expect something like:

items = items.OrderBy(o => o.OwnerID, comparer(o))

where comparing made by "index".

Thanks a lot.

4

There are 4 answers

3
Rodrigo Vedovato On BEST ANSWER

Assuming what you're trying to do is to order the items using the index property from the Owner object, something like this should do the job:

var joined = items.Join(owners, item => i.OwnerId, owner => o.OwnerId, (item, owner) => new
{
    Item = item,
    Index = owner.Index
});

var orderedItems = joined.OrderBy(j => j.Index).Select(j => j.Item);
0
AudioBubble On

Try this

//Initiate new object 
    Item items= new Items(); 
//Sort the items based on owner ID 
List myList = items.OrderBy(x => x.ownerID).ToList();  
0
Corak On

You can turn your Owner array into a Dictionary<int, int> like this:

Dictionary<int, int> ownerDict = owners.ToDictionary(x => x.ownerID, y => y.index);

And then use that to order the items like:

Item[] orderedItems = items.OrderBy(i => ownerDict[i.ownerID]).ToArray();

To order the items by the same index, the owners are ordered.

0
Peter B On

You can use LINQ .Select(), it will use a collection iterator to process the collection in its natural order. The lambda inside the Select can then be used to fetch each corresponding item from items. Like this:

var itemsInOrder = owners
    .Select(x => items.First(item => item.ownerId == x.ownerId))
    .ToArray();

This assumes that every ownerId can be found in items. If that is not true then you will have to extend it a little, e.g. like this:

var itemsInOrder = owners
    .Select(x => items.FirstOrDefault(item => item.ownerId == x.ownerId))
    .Where(item => item != null)
    .ToArray();