Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 518275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:55:14+00:00 2026-05-13T07:55:14+00:00

I have a list of items that have a partial order relation , i.

  • 0

I have a list of items that have a partial order relation, i. e, the list can be considered a partially ordered set. I want to sort this list in the same way as in this question. As correctly answered there, this is known as topological sorting.

There’s a reasonably simple known algorithm to solve the problem. I want a LINQ-like implementation of it.

I already tried to use OrderBy extension method, but I’m quite sure it’s not able to make topological sorting. The problem is that the IComparer<TKey> interface is not able to represent a partial order. This happens because the Compare method can return basically 3 kinds of values: zero, negative, and positive, meaning are-equal, is-less-than, and is-greater-then, respectively. A working solution would only be possible if there were a way to return are-unrelated.

From my biased point of view, the answer I’m looking for might be composed by an IPartialOrderComparer<T> interface and an extension method like this:

public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
    this IEnumerable<TSource> source,
    Func<TSource, TKey> keySelector,
    IPartialOrderComparer<TKey> comparer
);

How would this be implemented? How does the IPartialOrderComparer<T> interface would look like? Would you recommend a different approach? I’m eager to see it. Maybe there’s a nicer way to represent the partial order, I don’t know.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:55:14+00:00Added an answer on May 13, 2026 at 7:55 am

    I would suggest using the same IComparer interface, but writing the extension method so as to interpret 0 as not related. In a partial ordering, if elements a and b are equal their order doesn’t matter, like-wise if they are unrelated – you only have to order them with respect to elements with which they have defined relationships.

    Here’s an example that does a partial ordering of even and odd integers:

    namespace PartialOrdering
    {
        public static class Enumerable
        {
            public static IEnumerable<TSource> PartialOrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer)
            {
                List<TSource> list = new List<TSource>(source);
                while (list.Count > 0)
                {
                    TSource minimum = default(TSource);
                    TKey minimumKey = default(TKey);
                    foreach (TSource s in list)
                    {
                        TKey k = keySelector(s);
                        minimum = s;
                        minimumKey = k;
                        break;
                    }
                    foreach (TSource s in list)
                    {
                        TKey k = keySelector(s);
                        if (comparer.Compare(k, minimumKey) < 0)
                        {
                            minimum = s;
                            minimumKey = k;
                        }
                    }
                    yield return minimum;
                    list.Remove(minimum);
                }
                yield break;
            }
    
        }
        public class EvenOddPartialOrdering : IComparer<int>
        {
            public int Compare(int a, int b)
            {
                if (a % 2 != b % 2)
                    return 0;
                else if (a < b)
                    return -1;
                else if (a > b)
                    return 1;
                else return 0; //equal
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                IEnumerable<Int32> integers = new List<int> { 8, 4, 5, 7, 10, 3 };
                integers = integers.PartialOrderBy<Int32, Int32>(new Func<Int32, Int32>(delegate(int i) { return i; }), new EvenOddPartialOrdering());
            }
        }
    }
    

    Result: 4, 8, 3, 5, 7, 10

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 274k
  • Answers 274k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It sounds like you're doing your serverSocket.accept() call on the… May 13, 2026 at 2:23 pm
  • Editorial Team
    Editorial Team added an answer Instantiate a django.template.Template(), passing the string to use as a… May 13, 2026 at 2:23 pm
  • Editorial Team
    Editorial Team added an answer You should be using the ScriptManager to emit your javascript… May 13, 2026 at 2:23 pm

Related Questions

I have an HTML <TABLE> displaying a list of items in the rows of
I have a partial view that returns an HTML chunk of list items that
I have written a function that gets a given number of random records from
I have a generic list List<T> and I want to create a view that
I am starting out with Silverlight. I want to display a list of messages

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.