Home > Backend Development > C++ > How to Alphabetize Strings with Embedded Numbers While Preserving Numerical Order?

How to Alphabetize Strings with Embedded Numbers While Preserving Numerical Order?

Barbara Streisand
Release: 2024-12-30 10:16:14
Original
644 people have browsed it

How to Alphabetize Strings with Embedded Numbers While Preserving Numerical Order?

How to Sort Strings Alphabetically While Preserving Numeric Values

Sorting strings alphabetically can encounter challenges when some of the strings contain numeric characters. If numeric conversion is prohibited, an alternative approach is required to handle these cases effectively.

One solution is to utilize a custom comparer that distinguishes between numeric and non-numeric values. This comparer can be passed to the OrderBy method to customize the sorting criteria.

Here's an example implementation using the Enumerable.OrderBy method and a SemiNumericComparer class:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        string[] things = new string[] { "paul", "bob", "lauren", "007", "90", "101" };

        foreach (var thing in things.OrderBy(x => x, new SemiNumericComparer()))
        {    
            Console.WriteLine(thing);
        }
    }

    public class SemiNumericComparer: IComparer<string>
    {
        /// <summary>
        /// Determine if a string is a number
        /// </summary>
        /// <param name="value">String to test</param>
        /// <returns>True if numeric</returns>
        public static bool IsNumeric(string value)
        {
            return int.TryParse(value, out _);
        }

        /// <inheritdoc />
        public int Compare(string s1, string s2)
        {
            // Flags to indicate if strings are numeric
            var IsNumeric1 = IsNumeric(s1);
            var IsNumeric2 = IsNumeric(s2);

            // Handle numeric comparisons
            if (IsNumeric1 && IsNumeric2)
            {
                var i1 = Convert.ToInt32(s1);
                var i2 = Convert.ToInt32(s2);
                
                // Order by numerical value
                if (i1 > i2)
                {
                    return 1;
                }

                if (i1 < i2)
                {
                    return -1;
                }

                return 0;
            }

            // Handle cases where only one string is numeric
            if (IsNumeric1)
            {
                return -1;
            }

            if (IsNumeric2)
            {
                return 1;
            }

            // Default to alphabetical comparison
            return string.Compare(s1, s2, true, CultureInfo.InvariantCulture);
        }
    }
}
Copy after login

This code will output the strings in the following sorted order:

007
90
bob
lauren
paul
101
Copy after login

The above is the detailed content of How to Alphabetize Strings with Embedded Numbers While Preserving Numerical Order?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template