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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:42:35+00:00 2026-05-14T05:42:35+00:00

I’ve written the following IComparer but I need some help. I’m trying to sort

  • 0

I’ve written the following IComparer but I need some help. I’m trying to sort a list of numbers but some of the numbers may not have been filled in. I want these numbers to be sent to the end of the list at all times.. for example…

[EMPTY], 1, [EMPTY], 3, 2

would become…

1, 2, 3, [EMPTY], [EMPTY]

and reversed this would become…

3, 2, 1, [EMPTY], [EMPTY]

Any ideas?

        public int Compare(ListViewItem x, ListViewItem y)
    {
        int comparison = int.MinValue;
        ListViewItem.ListViewSubItem itemOne = x.SubItems[subItemIndex];
        ListViewItem.ListViewSubItem itemTwo = y.SubItems[subItemIndex];

        if (!string.IsNullOrEmpty(itemOne.Text) && !string.IsNullOrEmpty(itemTwo.Text))
        {
            uint itemOneComparison = uint.Parse(itemOne.Text);
            uint itemTwoComparison = uint.Parse(itemTwo.Text);

            comparison = itemOneComparison.CompareTo(itemTwoComparison);
        }
        else
        {
            // ALWAYS SEND TO BOTTOM/END OF LIST.
        }

        // Calculate correct return value based on object comparison.
        if (OrderOfSort == SortOrder.Descending)
        {
            // Descending sort is selected, return negative result of compare operation.
            comparison = (-comparison);
        }
        else if (OrderOfSort == SortOrder.None)
        {
            // Return '0' to indicate they are equal.
            comparison = 0;
        }

        return comparison;
    }

Cheers.

  • 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-14T05:42:36+00:00Added an answer on May 14, 2026 at 5:42 am

    Your logic is slightly off: your else will be entered if either of them are empty, but you only want the empty one to go to the end of the list, not the non-empty one. Something like this should work:

    public int Compare(ListViewItem x, ListViewItem y)
    {
        ListViewItem.ListViewSubItem itemOne = x.SubItems[subItemIndex];
        ListViewItem.ListViewSubItem itemTwo = y.SubItems[subItemIndex];
    
        // if they're both empty, return 0
        if (string.IsNullOrEmpty(itemOne.Text) && string.IsNullOrEmpty(itemTwo.Text))
            return 0;
    
        // if itemOne is empty, it comes second
        if (string.IsNullOrEmpty(itemOne.Text))
            return 1;
    
        // if itemTwo is empty, it comes second
        if (string.IsNullOrEmpty(itemTwo.Text)
            return -1;
    
        uint itemOneComparison = uint.Parse(itemOne.Text);
        uint itemTwoComparison = uint.Parse(itemTwo.Text);
    
        // Calculate correct return value based on object comparison.
        int comparison = itemOneComparison.CompareTo(itemTwoComparison);
        if (OrderOfSort == SortOrder.Descending)
            comparison = (-comparison);
    
        return comparison;
    }
    

    (I might’ve got the “1” and “-1” for when they’re empty back to front, I can never remember 🙂

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have an array which has BIG numbers and small numbers in it. I
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I need to clean up various Word 'smart' characters in user input, including but
I am trying to loop through a bunch of documents I have to put

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.