You must Sign In to post a response.
  • Category: .NET

    How to sort string list?

    I am working on one project and now i am stuck on very silly program i.e sorting list.
    I have list of items range between 0-500 . and items like (e.g "45848^kjf,10" , "450240^jdk,20" , "448278^dj,30").
    My current code sort list in decending order but the order gets wrong result . i.e

    1. 45848^kjf,10
    2. 450240^jdk,20
    3. 448278^dj,30

    I want to sort list items with respective to the number before "^" this symbol and 45848 is smaller than below two numbers so, my expected result is

    1. 450240^jdk,20
    2. 448278^dj,30
    3. 45848^kjf,10

    How can i get this result? here is my code,

    listcode.Sort(); // listcode is my string list
  • #768306
    I don't understand your sorting logic, it is ascending or descending ? if you have to sort number before '^' then why '45848' is come after '448278'
    How you have played your logic.
    Please elaborate more.

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #768312

    Is that sorting a Number or Varchar Type?
    You have specified number and power of some characters.
    According to sorting we have numerous number of sorting techniques like Bubble Sort, Heap Sort, Insert Sort and many more. So if you clearly specify you requirement we can help you out.

    Provide us like what is Input and what is expected output


  • #768317
    You can try to use Comparer.Create method to shorting with a lambda expression. Example of code snippet
     list.Sort(1, 2, Comparer<int>.Create((i1, i2) => i1.CompareTo(i2)));

    To create comparer:
     public class FunctionComparer<T>: IComparer<T>
    private readonly Function<T, T, int> Function;
    public FunctionComparer(Function<T, T, int> comparerFunction)
    this.Function = comparerFunction;
    Public int Compare(T x, T y)
    return this.Function(x, y);

    Useful referral link:

  • Sign In to post your comments