-3

I have list of alphanumeric, I want this order first number and then character in descending order.

EH40, DH40, DH36,AH40, AH36,EH27S,  DH32,EH32, AH32,  DH27S, EH36

Expected result:

EH40, DH40, AH40, EH36, DH36, AH36 ,EH32, DH32 and so on
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • If you always have the same pattern (two-digit letters, two-digit number, optional letter), you can just use a string sort and reverse the list. – Heinzi Feb 02 '22 at 14:56
  • @Heinzi But then it would be ordered by letters before numbers. – Johnathan Barclay Feb 02 '22 at 14:58
  • 1
    The "DH27S" does not really fit here, or? – Klaus Gütter Feb 02 '22 at 15:00
  • 2
    your description of the desired result doesn't match your example for the desired result (the latter being _simple_ alphanumeric order). also: welcome to stackoverflow. i recommend [taking the tour](https://stackoverflow.com/tour), as well as reading [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and [what's on topic](https://stackoverflow.com/help/on-topic). – Franz Gleichmann Feb 02 '22 at 15:02
  • Possible duplicate: https://stackoverflow.com/Questions/248603/Natural-Sort-Order-in-C-Sharp) – Matthew Watson Feb 02 '22 at 15:07
  • You can use Icomparable and regex. I have solution but posting is closed and cannot post. – jdweng Feb 02 '22 at 15:43
  • @JohnathanBarclay: Good point, I misread the question. – Heinzi Feb 02 '22 at 16:06
  • Considering your expected result, I think you mean _I want this ordered **first by number in descending order** and then by character in descending order._. If that's the case, please edit your question to specify your requirements properly and explain how the "odd" items (EH27**S**, DH27**S**) should be compared to the other items. – Astrid E. Feb 02 '22 at 16:08

1 Answers1

1

Try following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;


namespace ConsoleApplication11
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string[] input = {"EH40", "DH40", "DH36","AH40", "AH36","EH27S", "DH32","EH32", "AH32", "DH27S", "EH36"};

            string[] results = input.Select(x => new AlphaNumeric(x)).OrderBy(x => x).Select(x => x.input).ToArray();

          
        }
  
    }
    public class AlphaNumeric : IComparable<AlphaNumeric>
    {
        string prefix { get; set; }
        int number { get; set; }
        public string input { get; set; }
        static string pattern = @"(?'prefix'.*)(?'number'\d+)[^\d]*$";
        public AlphaNumeric(string input)
        {
            this.input = input;
            Match match = Regex.Match(input, pattern, RegexOptions.RightToLeft);
            string numberStr = match.Groups["number"].Value;
            number = int.Parse(numberStr);
            prefix = match.Groups["prefix"].Value;

        }
        public int CompareTo(AlphaNumeric other)
        {
            int results = 0;

            if (this.number != other.number)
            {
                results = this.number.CompareTo(other.number);
            }
            else
            {
                if (this.prefix == other.prefix)
                {
                    results = this.input.CompareTo(other.input);
                }
                else
                {
                    results = this.prefix.CompareTo(other.prefix);
                }
            }

            return results;
        }
    }

 
}
jdweng
  • 33,250
  • 2
  • 15
  • 20