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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:10:07+00:00 2026-05-27T09:10:07+00:00

We have some columns in a grid that contain numbers, strings, or strings that

  • 0

We have some columns in a grid that contain numbers, strings, or strings that start with a number and the users expect them to sort in a sensible way. (The format depends on the customer so we don’t know the formats the strings will be in)

Is there a pre-canned” implementation of IComparable that does something like this?

  • 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-27T09:10:08+00:00Added an answer on May 27, 2026 at 9:10 am

    here is a fast alphanumeric sort (can be used for other sorts with numerics too).

    C# Alphanumeric Sorting http://www.dotnetperls.com/alphanumeric-sorting

    var unordered = new[] { "100F", "50F", "SR100", "SR9" };
    var ordered = unordered.OrderBy(s => s, new AlphanumComparatorFast());
    

    and here is a nice article about the problem:

    Sorting for Humans : Natural Sort Order http://www.codinghorror.com/blog/2007/12/sorting-for-humans-natural-sort-order.html

    Here is the AlphanumComparatorFast class in case the above link stops working The end of the page states “It can be used in any program with no restrictions” and the comment at the start of the code also indicates it is free for use:

    // NOTE: This code is free to use in any program.
    // ... It was developed by Dot Net Perls.
    
    public class AlphanumComparatorFast : IComparer
    {
        public int Compare(object x, object y)
        {
            string s1 = x as string;
            if (s1 == null)
            {
                return 0;
            }
            string s2 = y as string;
            if (s2 == null)
            {
                return 0;
            }
    
            int len1 = s1.Length;
            int len2 = s2.Length;
            int marker1 = 0;
            int marker2 = 0;
    
            // Walk through two the strings with two markers.
            while (marker1 < len1 && marker2 < len2)
            {
                char ch1 = s1[marker1];
                char ch2 = s2[marker2];
    
                // Some buffers we can build up characters in for each chunk.
                char[] space1 = new char[len1];
                int loc1 = 0;
                char[] space2 = new char[len2];
                int loc2 = 0;
    
                // Walk through all following characters that are digits or
                // characters in BOTH strings starting at the appropriate marker.
                // Collect char arrays.
                do
                {
                    space1[loc1++] = ch1;
                    marker1++;
    
                    if (marker1 < len1)
                    {
                        ch1 = s1[marker1];
                    }
                    else
                    {
                        break;
                    }
                } while (char.IsDigit(ch1) == char.IsDigit(space1[0]));
    
                do
                {
                    space2[loc2++] = ch2;
                    marker2++;
    
                    if (marker2 < len2)
                    {
                        ch2 = s2[marker2];
                    }
                    else
                    {
                        break;
                    }
                } while (char.IsDigit(ch2) == char.IsDigit(space2[0]));
    
                // If we have collected numbers, compare them numerically.
                // Otherwise, if we have strings, compare them alphabetically.
                string str1 = new string(space1);
                string str2 = new string(space2);
    
                int result;
    
                if (char.IsDigit(space1[0]) && char.IsDigit(space2[0]))
                {
                    int thisNumericChunk = int.Parse(str1);
                    int thatNumericChunk = int.Parse(str2);
                    result = thisNumericChunk.CompareTo(thatNumericChunk);
                }
                else
                {
                    result = str1.CompareTo(str2);
                }
    
                if (result != 0)
                {
                    return result;
                }
            }
            return len1 - len2;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataGrid, and in that grid, some of the columns are marked
I have a data grid where users can drag columns and reposition them. But
I have a gridview that has some 20 columns and 1000 rows. The grid
I have one grid view that contains some template columns. In this, the first
We have some columns with data that must always be in uppercase to ensure
I've got some Columns in a DataGridView that have their Binding property set to
i have one database, and it contains some columns. My requirement is that how
I have a data frame where some of the columns contain NA values. How
I have a table that, some of its columns are unknown at compile time.
I have a grid with some columns to be edited. One of the columns

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.