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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:40:13+00:00 2026-06-18T00:40:13+00:00

i have a class: public class Essay { public int ID{get;set;} public string Name{get;set;}

  • 0

i have a class:

public class Essay
    {   
            public int ID{get;set;}
            public string Name{get;set;}
    }

and list of Essay type

List<Essay> essays=new List<Essay>();

on the name property contains numbers and letters.

i want to sort the list by the name property

for example:

essays=
{1,"ccccc"},
{2,"aaaa"},
{3,"bbbb"},
{4,"10"},
{5,"1"},
{6,"2"},
{7,"1a"}

i want to sort:

essays=
{2,"aaaa"},
{3,"bbbb"},
{1,"ccccc"},
{5,"1"},
{7,"1a"},
{6,"2"},
{4,"10"}

how i do it?

thank to all.

  • 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-06-18T00:40:14+00:00Added an answer on June 18, 2026 at 12:40 am

    There are several elements to the answer.

    The first part is being able to in-place sort a List using Sort() and a lambda comparison method. That’s solved by using an extension method for IList and a helper “ComparisonDelegator” class. Combining those, it’s possible to pass a lambda to List.Sort().

    The second part has been addressed in another post here (which I have upvoted) and the code from which I have shamelessly pasted into the AlphanumComparator class in this answer.

    (As a side note, I should point out that all the Linq examples posted elsewhere in this thread make a COPY of the list. This is fine for short lists, but if you have a long list it can cause performance problems. The solution presented here does NOT make a copy of the list.)

    Putting it all together, we get the following code, which outputs:

    ID=2, Name=aaaa
    ID=3, Name=bbbb
    ID=1, Name=ccccc
    ID=5, Name=1
    ID=7, Name=1a
    ID=6, Name=2
    ID=4, Name=10
    

    And the full code sample (compilable as a console application):

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Text;
    
    
    namespace Demo
    {
        public static class Program
        {
            public static void Main()
            {
                var list = new List<Essay>
                {
                    new Essay {ID=1, Name="ccccc"},
                    new Essay {ID=2, Name="aaaa"},
                    new Essay {ID=3, Name="bbbb"},
                    new Essay {ID=4, Name="10"},
                    new Essay {ID=5, Name="1"},
                    new Essay {ID=6, Name="2"},
                    new Essay {ID=7, Name="1a"}                
                };
    
                var comp = new AlphanumComparator();
    
                list.Sort((lhs, rhs) => comp.Compare(lhs.Name, rhs.Name));
    
                foreach (var essay in list)
                {
                    Console.WriteLine("ID={0}, Name={1}", essay.ID, essay.Name);
                }
            }
        }
    
        public class Essay
        {
            public int ID
            {
                get;
                set;
            }
    
            public string Name
            {
                get;
                set;
            }
        }
    
        /// <summary>Extensions for IList{T}</summary>
    
        public static class ListExt
        {
            /// <summary> Sorts an IList{T} in place. </summary>
    
            public static void Sort<T>(this IList<T> list, Comparison<T> comparison)
            {
                ArrayList.Adapter((IList)list).Sort(new ComparisonDelegator<T>(comparison));
            }
        }
    
        /// <summary>
        /// Provides a mechanism for easily converting a Comparison&lt;&gt; delegate (or lambda) to an IComparer&lt;&gt;.
        /// This can be used for List.BinarySearch(), for example.
        /// </summary>
        /// <typeparam name="T">The type of items to be compared.</typeparam>
    
        public sealed class ComparisonDelegator<T>: IComparer<T>, IComparer
        {
            /// <summary>Create from a Comparison&lt;&gt; delegate.</summary>
            /// <param name="comparison">A Comparison&lt;&gt; delegate.</param>
    
            public ComparisonDelegator(Comparison<T> comparison)
            {
                this._comparison = comparison;
            }
    
            /// <summary>Implements the IComparer.Compare() method.</summary>
    
            public int Compare(T x, T y)
            {
                return _comparison(x, y);
            }
    
            /// <summary>Implements the IComparer.Compare() method.</summary>
    
            public int Compare(object x, object y)
            {
                return _comparison((T)x, (T)y);
            }
    
            /// <summary>Used to store the Comparison delegate.</summary>
    
            private readonly Comparison<T> _comparison;
        }
    
        /// <summary>
        /// Special class to sort strings "naturally", 
        /// but to place non-numeric items *before* numeric items.
        /// </summary>
    
        public class AlphanumComparator : IComparer
        {
            private enum ChunkType {Alphanumeric, Numeric};
            private bool InChunk(char ch, char otherCh)
            {
                ChunkType type = ChunkType.Alphanumeric;
    
                if (char.IsDigit(otherCh))
                {
                    type = ChunkType.Numeric;
                }
    
                if ((type == ChunkType.Alphanumeric && char.IsDigit(ch))
                    || (type == ChunkType.Numeric && !char.IsDigit(ch)))
                {
                    return false;
                }
    
                return true;
            }
    
            public int Compare(object x, object y)
            {
                String s1 = x as string;
                String s2 = y as string;
                if (s1 == null || s2 == null)
                {
                    return 0;
                }
    
                int thisMarker = 0, thisNumericChunk = 0;
                int thatMarker = 0, thatNumericChunk = 0;
    
                while ((thisMarker < s1.Length) || (thatMarker < s2.Length))
                {
                    if (thisMarker >= s1.Length)
                    {
                        return -1;
                    }
                    else if (thatMarker >= s2.Length)
                    {
                        return 1;
                    }
                    char thisCh = s1[thisMarker];
                    char thatCh = s2[thatMarker];
    
                    StringBuilder thisChunk = new StringBuilder();
                    StringBuilder thatChunk = new StringBuilder();
    
                    while ((thisMarker < s1.Length) && (thisChunk.Length==0 ||InChunk(thisCh, thisChunk[0])))
                    {
                        thisChunk.Append(thisCh);
                        thisMarker++;
    
                        if (thisMarker < s1.Length)
                        {
                            thisCh = s1[thisMarker];
                        }
                    }
    
                    while ((thatMarker < s2.Length) && (thatChunk.Length==0 ||InChunk(thatCh, thatChunk[0])))
                    {
                        thatChunk.Append(thatCh);
                        thatMarker++;
    
                        if (thatMarker < s2.Length)
                        {
                            thatCh = s2[thatMarker];
                        }
                    }
    
                    int result = 0;
                    // If both chunks contain numeric characters, sort them numerically
                    if (char.IsDigit(thisChunk[0]) && char.IsDigit(thatChunk[0]))
                    {
                        thisNumericChunk = Convert.ToInt32(thisChunk.ToString());
                        thatNumericChunk = Convert.ToInt32(thatChunk.ToString());
    
                        if (thisNumericChunk < thatNumericChunk)
                        {
                            result = -1;
                        }
    
                        if (thisNumericChunk > thatNumericChunk)
                        {
                            result = 1;
                        }
                    }
                    else if (char.IsDigit(thisChunk[0]) && !char.IsDigit(thatChunk[0]))
                    {
                        return 1; // Ensure that non-numeric sorts before numeric.
                    }
                    else if (!char.IsDigit(thisChunk[0]) && char.IsDigit(thatChunk[0]))
                    {
                        return -1;  // Ensure that non-numeric sorts before numeric.
                    }
                    else
                    {
                        result = thisChunk.ToString().CompareTo(thatChunk.ToString());
                    }
    
                    if (result != 0)
                    {
                        return result;
                    }
                }
    
                return 0;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class: public class CarList { public int quantity{get;set;} public List<Car> Cars
I have this class public class Audit { public string name { get; set;}
I have a class: public class LevelInfo { public int Id { get; set;
i have this class public class Image { public string url { get; set;
I have class: public class LoginResponse { public String SessionID { get; private set;
I have class Student { public List<Degree> Degrees {get;set;} } class Degree { public
I have class Marker class Marker { public double Position {get; set;} public string
I have a class: public class ClientModelData { public int clientID { get; set;
I have class say for example public class Item { int price; String name;
I have class public class Class2 { public string myName { get; set; }

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.