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

  • SEARCH
  • Home
  • 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 8990673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:33:03+00:00 2026-06-15T22:33:03+00:00

My question is about the IComparer interface, I never worked with it before, so

  • 0

My question is about the IComparer interface, I never worked with it before, so I hope you can help me set up everything right.

I have to use the interface to sort an list of own objects by the exact sequence of another List<int>.
I couldn’t find anything usefull with that problem on the net, everything I found were linq statements, that I can not use.

Here is the example code:

public class What : IComparer<What>
{
    public int ID { get; set; }
    public string Ever { get; set; }

    public What(int x_ID, string x_Ever)
    {
        ID = x_ID;
        Ever = x_Ever;
    }

    public int Compare(What x, What y)
    {
        return x.ID.CompareTo(y.ID);
    }
}

Some data to work with:

List<What> WhatList = new List<What>()
{
    new What(4, "there"),
    new What(7, "are"), 
    new What(2, "doing"),
    new What(12, "you"),
    new What(78, "Hey"),
    new What(63, "?")
};

And the list with the correct order:

List<int> OrderByList = new List<int>() { 78, 4, 63, 7, 12, 2 };

So now how can I tell IComparer to sort by the OrderByList?
I really got no clue how to do that, I know this would be pretty easy with linq, but I don’t have the opportunity to use it.

  • 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-06-15T22:33:05+00:00Added an answer on June 15, 2026 at 10:33 pm

    There are a few tings wrong with your code as it currently stands. If you look at the docs for IComparer<T> you’ll see that the T is what you’re saying you are going to compare. In your code this is Test but you go on to code for comparisons of What – this means that your code will not compile. See here – error message is:

    ‘Rextester.What’ does not implement interface member ‘System.Collections.Generic.IComparer.Compare(Rextester.Test, Rextester.Test)’
    (Ignore the “Rextester” bit there!).

    With all that said and done, you should implement a WhatComparer:

    public class WhatComparer : IComparer<What>
    {
        private List<int> orderBy;
        public WhatComparer(List<int> orderBy)
        {
            this.orderBy = orderBy;
        }
    
        public int Compare(What x, What y)
        {
            return orderBy.IndexOf(x.ID).CompareTo(orderBy.IndexOf(y.ID));
        }
    }
    

    And use that for ordering:

     WhatList.Sort(new WhatComparer(OrderByList));
    

    Live example: http://rextester.com/BZKO33641

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

Sidebar

Related Questions

Question about GridView sorting in VB.NET: I have a GridView with AutoGenerateColumns = True
Question about controllers. Can controller call it`s own class methods inside an action? EDIT:
Question about how AppFabric's cache feature can be used. I apologize for asking a
Simple question about Jquery-UI sortable lists (http://jqueryui.com/demos/sortable/#default) I have made: <ul id=sortable> <li class=ui-state-default>An
My question about My SQL is, I have a table where users enter time
Simple question about best-practice. I'm using Kohana... is it okay to use helpers in
A question about symfony form validators. For example I have an url field. If
Quick question about cursors (in particular Oracle cursors). Let's say I have a table
A question about C++ programming. Can you do in C++ something like this: Lecture
I have a question about Javascript widgets. The widget I am working on simply

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.