0

I created a class called tagNoMatchList that has fields tag, col, and msg.

public class tagErrorsClass
{
    public string tag { get; set; }
    public string col { get; set; }
    public string msg { get; set; }
}

I created a List of tagNoMatchList as shown below.

var tagNoMatchList = new List<tagErrorsClass>();

Now want to sort the list on col. The code below sorts the list, but not the way that I want.

tagNoMatchList.Sort((x, y) => x.col.CompareTo(y.col));

If col contains "A29", "A21", "A52", "A16", and "A6", after sorting I get "A16", "A21", "A29", "A52", "A6". I want "A6" at the beginning of the list not at the end. Do I have to split the col string into letter (A) and number and sort each, or is there a built in comparison that will make this easier?

Dan Z
  • 101
  • 13
  • 2
    "Do I have to split the col string into letter (A) and number and sort each" Yeap. If you want numeric-based comparison, you will need **numbers**. – MakePeaceGreatAgain Jun 04 '19 at 18:14
  • Research *NaturalSort* - numerals mixed in with letters do not sort as *numbers* – Ňɏssa Pøngjǣrdenlarp Jun 04 '19 at 18:15
  • what is your desired output – Praneet Nadkar Jun 04 '19 at 18:18
  • 1
    To split hairs then, this question isn't about sorting a list based on a field in a class. It's about how to create custom sorting for a collection of strings. – Scott Hannen Jun 04 '19 at 18:19
  • 1
    Is it **always** one letter followed by only a number? – Idle_Mind Jun 04 '19 at 18:20
  • 1
    Look into the IComparer Interface https://learn.microsoft.com/en-us/dotnet/api/system.collections.icomparer?view=netframework-4.8 Possible duplicate of this: https://stackoverflow.com/questions/1032775/sorting-mixed-numbers-and-strings – Jake Steffen Jun 04 '19 at 18:23
  • In the middle of typing an answer out when it closed, but [here is a solution you can use](https://dotnetfiddle.net/oHlQ38) if the pattern is a single letter followed by some number of digits (up to a value of 10000). – Ron Beyer Jun 04 '19 at 18:38
  • Sorry, but I disagree that this question is answered by any of the links provided. I don't have a simple List. I have a List of tagErrorClass, which contains three strings. I need to sort the List based on one of the fields in the class and keep the other two fields together with col. Can't simply sort col and throw away the rest of the data. I would be happy to provide more code. I figured that the class definition, usage of the class, and code used to sort were enough. Thanks. – Dan Z Jun 04 '19 at 21:00

1 Answers1

-1

There is no built-in comparison for this. You will need to write code to handle the comparison.

However, there's not enough information yet in the question for us to provide this code for you, namely whether or not the col will always begin with A, or whether it could have other (or no) prefix values, and how that might impact the sorting.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 1
    "However, there's not enough information yet in the question for us" And why do we answer it then? – MakePeaceGreatAgain Jun 04 '19 at 18:51
  • @HimBromBeere Because the first paragraph effectively answered the (and I quote) "is there a built in comparison that will make this easier" question, even with no code. – Joel Coehoorn Jun 04 '19 at 19:09
  • Thanks for all of the replies. Wikipedia says "Functionality to sort by natural sort order is built into many programming languages and libraries". It might be built into C#, but it isn't very obvious! :o) In this case, the string will always start with A. One thing that complicates this is that I can't simply sort the col. because it needs to stay with the other two values in the class (tag and msg). – Dan Z Jun 04 '19 at 20:22
  • 1
    @DanZ Natural sorting in C# is fine. What you want **is not a natural sort!** You want a hybrid between lexical and numeric. For that, you must write your own comparer. – Joel Coehoorn Jun 04 '19 at 21:14