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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:49:02+00:00 2026-06-15T03:49:02+00:00

I need to sort an employee list based on predefined uniqueIds. In simple words,

  • 0

I need to sort an employee list based on predefined uniqueIds.

In simple words, consider a list of employee Ids 1 to 10 in random order.

I have a predefined rule that says order employee objects in 2, 8, 1, 4, 6 And if any employee UId is not in range [1,10] put them at the end of list…(any order).

I wrote following code using IComparer<Employee>.

public class Employee
    {
        public int UId { get; set; }
        public string Name { get; set; }        
    }

    class Comparision : IComparer<Employee>
    {
        List<int> referenceKeys = new List<int> { 2, 8, 1, 4, 6 };
        public int Compare(Employee thisOne, Employee otherOne)
        {
            var otherIndex = referenceKeys.IndexOf(otherOne.UId);
            var thisIndex = referenceKeys.IndexOf(thisOne.UId);
            if (thisIndex > otherIndex)
            {
                return 1;
            }
            else if (thisIndex < otherIndex)
            {
                return -1;
            }
            else
            {
                //if uid not found in reference list treat both employee obj as equal
                return 0;
            }
        }
    }
    class CustomSorting
    {
        public static 
        List<Employee> employees = new List<Employee>
        {
            new Employee{UId=1, Name="Ram"},
            new Employee{UId=2 , Name="Shyam"},
            new Employee{UId=3 , Name="Krishna"},
            new Employee{UId=4 , Name="Gopal"},
            new Employee{UId=5 , Name="Yadav"},
            new Employee{UId=6 , Name="Vishnu"},
            new Employee{UId=7 , Name="Hari"},
            new Employee{UId=8 , Name="Kanha"},
        };

        void sort()
        {
            employees.Sort(new Comparision());
        }

        static void Main()
        {
            new CustomSorting().sort();
        }
    }

I have been able to sort the list, with following result-

(5, 7, 3), 2, 8, 1, 4, 6 ==> 5, 7, 3 are not listed in reference key, so should appear in last, any order..

But items not found in my reference keys, are sorted first. I need to put them at the end.

For such a scenario, is IComparer, best way to go for ?

  • 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-15T03:49:03+00:00Added an answer on June 15, 2026 at 3:49 am

    var otherIndex = referenceKeys.IndexOf(otherOne.UId); will return -1 if the item isn’t found, which will be less than any found value.

    You want all not found items to be greater than any found value, so just add:

    if(otherIndex == -1) otherIndex = int.MaxValue;
    if(thisIndex == -1) thisIndex = int.MaxValue;
    

    On a side note, you can simplify the remainder of the method by just using:

    return thisIndex.CompareTo(otherIndex);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to sort list of strings in the alphabetical order: List<String> list =
I need to sort a List based on the difference between the strings in
I need to sort a list of tuples by first item in descending order,
I need to sort nodes in xml. I have the following code which successfully
I need to sort then print the result increasing and decreasing. I have some
I have a list of products coming from a repository. Simple enough. Now I
I need to sort the results I get back from apache solr based on
I need to sort many files and then dump into many files in order
I need to sort an array containing arrays in ascending numerical order. The data
I need to sort object list using comparators I need nested type sorting such

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.