Sorting a list in c# based on dependant values in another list

别来无恙 提交于 2020-03-03 06:02:47

问题


I'm trying to sort a list of items based on the order of the occurrence in another list.

Below is the basic structure:

public class ItemList
{
    public List<Item> items;

    public ItemList(string myValue)
    {
        items = new List<Item>();
    }

    public void addItem (string myValue)
    {
        var newItem = new Item(myValue);
        items.Add(newItem);
    }

    public class Item
    {
        public List<string> myLinkedValues;
        public string myValue;

        public Item(string myValue)
        {
            myLinkedValues = new List<string>();
        }

        public void addLinkedItem(string linkedItem)
        {
            myLinkedValues.Add(linkedItem);
        }
    }
} 

So if my list had the following values (myValue and myLinkedValues):

A
 C
B
 A C
C
D
 C B

I'm trying to sort it so dependant values are first like this:

C
A
 C
B
 A C
D
 C B

Basically the way I do it is quite slow, so I was wondering if there is some quicker way of doing this.

Edit, basics of my sort:

public void Sort()
        {
            var changed = true;
            while (changed)
            {
                changed = false;
                var sortedList = new List<Item>();
                foreach (Item item in items)
                {
                    var minimumPosition = -1;
                    var parents = Linked.FindAll(myList => myList.isLinked(item.myValue);
                    foreach (Item parent in parents)
                    {
                        var parentPosition = sortedList.IndexOf(parent);
                        if (minimumPosition == -1 || (parentPosition < minimumPosition && parentPosition != -1))
                        {
                            minimumPosition = parentPosition;
                        }
                    }

                    if (minimumPosition == -1)
                    {
                        sortedList.Add(item);
                    }
                    else
                    {
                        sortedList.Insert(minimumPosition, item);
                        changed = true;
                    }
                }
                items = sortedList;
            }
        }

    public bool isLinked(string myValue)
    {
        var isLinked = false;
            isLinked = items.Any(check => check == myValue;
            return isLinked;
    }

回答1:


To compare two sequences we'll first need to create a comparer that can compare sequences. It's reasonably straightforward to do. Try to get the next item from both sequences, if neither has another item, the sequences are equal, if one has ended and the other hasn't that one is "less" than the other, if both have another item then compare those items, and if they're the same keep looping.

public class SequenceComparer<T> : IComparer<IEnumerable<T>>
{
    private IComparer<T> comparer;
    public SequenceComparer(IComparer<T> compareer = null)
    {
        this.comparer = comparer ?? Comparer<T>.Default;
    }

    public int Compare(IEnumerable<T> x, IEnumerable<T> y)
    {
        using (var first = x.GetEnumerator())
        using (var second = x.GetEnumerator())
        {
            while (true)
            {
                var firstHasMore = first.MoveNext();
                var secondHasMore = second.MoveNext();
                if (!firstHasMore && !secondHasMore)
                    return 0;
                var lengthComparison = firstHasMore.CompareTo(secondHasMore);
                if (lengthComparison != 0)
                    return lengthComparison;
                var nextComparison = comparer.Compare(first.Current, second.Current);
                if (nextComparison != 0)
                    return nextComparison;
            }
        }
    }
}

With this comparer sorting the list is rather easy:

var sorted = items.OrderBy(item => item.myLinkedValues, 
    new SequenceComparer<string>());



回答2:


Try this and sorry if i have understood your requirement wrongly.

Consider below as your dependent collection with list of string.

List<string> dependentCollection= new List<string>{ "A","B" "C", "D", "E","F","G"};

Here your original collection

List<string> myValues = new List<string>{"C","G","B","A"}; 
var sortedList= myValues.OrderBy(i => dependentCollection.IndexOf(i.ToString()));

Output will like below,

A B C G



来源:https://stackoverflow.com/questions/28587580/sorting-a-list-in-c-sharp-based-on-dependant-values-in-another-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!