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 8198053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:52:11+00:00 2026-06-07T05:52:11+00:00

In my app, I have several comparison methods. I would like the user to

  • 0

In my app, I have several comparison methods. I would like the user to be able to select the sorting method to use. Ideally, I would like to set a delegate and it is updated based on the user’s selection. This way, I can keep the code generic using List.Sort(delegate).

This is my first time attempting to use C# delegates and I’m running into syntax errors. Here is what I have so far:

The delegate:

private delegate int SortVideos(VideoData x, VideoData y);
private SortVideos sortVideos;

In the class constructor:

sortVideos = Sorting.VideoPerformanceDescending;

The comparison method in the public static Sorting class (which works when I call it directly):

public static int VideoPerformanceDescending(VideoData x, VideoData y)
{
    *code statements*
    *return -1, 0, or 1*
}

Failed syntax that complains of “some invalid arguments”:

videos.Sort(sortVideos);

Ultimately, I would like to change “sortVideos” to point to the method of choice. “videos” is a list of type VideoData. What am I doing wrong?

  • 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-07T05:52:12+00:00Added an answer on June 7, 2026 at 5:52 am

    The List<T> accepts a delegate of type Comparison<T> so you cannot define your own delegate, you just need to reuse the delegate Comparison<T>.

    private static Comparison<VideoData> sortVideos;
    
    static void Main(string[] args)
    {
        sortVideos = VideoPerformanceDescending;
    
        var videos = new List<VideoData>();
    
        videos.Sort(sortVideos);
    }
    

    Expanding the answer to also contemplate the user selection part, you could store the available options in a dictionary and then in the UI allow the user to select the sort algorithm by selecting the key to the dictionary.

    private static Dictionary<string, Comparison<VideoData>> sortAlgorithms;
    
    static void Main(string[] args)
    {
        var videos = new List<VideoData>();
    
        var sortAlgorithms = new Dictionary<string, Comparison<VideoData>>();
    
        sortAlgorithms.Add("PerformanceAscending", VideoPerformanceAscending);
        sortAlgorithms.Add("PerformanceDescending", VideoPerformanceDescending);
    
        var userSort = sortAlgorithms[GetUserSortAlgorithmKey()];
    
        videos.Sort(userSort);
    }
    
    private static string GetUserSortAlgorithmKey()
    {
        throw new NotImplementedException();
    }
    
    private static int VideoPerformanceDescending(VideoData x, VideoData y)
    {
        throw new NotImplementedException();
    }
    
    private static int VideoPerformanceAscending(VideoData x, VideoData y)
    {
        throw new NotImplementedException();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app that have several views. In one view I would like
I have several active record models in a rails app and I would like
In my iOS app I have several UIElement s that can process user input:
In a C# Winforms-App I have several user settings stored. Is there an easy
I have several parts in my app where I use custom table view cells.
I have several properties related to the configuration of my app that I'd like
Writing a PHP app and have several classes that only have static methods (no
I have several Groovy beans defined within my app context file, like so: <lang:groovy
I have several arrays of arrays or arrays of dicts that I would like
In a Rails app I have several integer attributes on a model. A user

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.