1

I would like to sort my list of strings by their length in place

var weasleys = new List<string>{"Bill", "Charlie", "Percy", "Fred", "George", "Ron", "Ginny"};

I know how to do it with OrderBy, but this returns a copy.

weasleys.OrderBy(x => x.Length)

I would prefer to sort in place with List<T>.Sort. How?

2
  • why can't you just use OrderBy like you did? Commented Nov 26, 2013 at 12:04
  • You must use custom comparer to your T class. Commented Nov 26, 2013 at 12:04

4 Answers 4

7

There's a Sort(Comparison<T>) overload that takes a Comparison<T> delegate. With that, you can just use the CompareTo method on the respective Length values:

weasleys.Sort((x, y) => x.Length.CompareTo(y.Length));

Note that unlike the LINQ OrderBy method, the Sort will alter your weasleys List in-place if that is your intent.

Sign up to request clarification or add additional context in comments.

Comments

2

You can do this with Array List...

public class SortStringLength : IComparer
    {
        public int Compare(Object s1, Object s2)
        {
            if (s1.ToString().Length == s2.ToString().Length)
                return String.CompareOrdinal(s1.ToString(), s2.ToString());
            if (s1.ToString().Length > s2.ToString().Length)
                return 1;
            return -1;
        }
    }

And, then to use the IComparer...

static void Main(string[] args)
        {
            var weasleys = new ArrayList { "Bill", "Charlie", "Percy", 
                  "Fred", "George", "Ron", "Ginny" };
            var sortLength = new SortStringLength();
            weasleys.Sort(sortLength);

            foreach (var weasley in weasleys)
            {
                Console.WriteLine(weasley);
            }

            Console.ReadLine();
        }

With List...

public class SortStringLength : IComparer<string>
{
    public int Compare(string x, string y)
    {
        if (x.Length == y.Length)
            return String.CompareOrdinal(x, y);
        if (x.Length > y.Length)
            return 1;
        return -1;
    }
}

and usage...

static void Main(string[] args)
        {
            var weasleys = new List<string> { "Bill", "Charlie", "Percy", 
                       "Fred", "George", "Ron", "Ginny" };
            var sortLength = new SortStringLength();
            weasleys.Sort(sortLength);

            foreach (var weasley in weasleys)
            {
                Console.WriteLine(weasley);
            }

            Console.ReadLine();
        }

Comments

1

This should do it

var weasleys = new List<string>{"Bill", "Charlie", "Percy", "Fred", "George", "Ron", "Ginny"};
var t = weasleys.OrderBy(x => x.Length).ToList<string>();

Comments

-1
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

public class Program
{
    public static void Main()
    {
        List<string> dictionary = new List<string>() { "aaa", "aaaa", "a", "aa" };
        var orderedDic = dictionary.Select(x => new
          {
            val = x,
            length = x.Length
          }).OrderBy(x => x.length).ToList();
        dictionary = orderedDic.Select(x => x.val).ToList();
        Console.WriteLine(string.Join(",", dictionary));
     }
}

The approach which I have used here

  1. created a list of anonymous objects with val and length property.
  2. Order the list on length.

2 Comments

I suspect that your answer was downvoted because it is much more complicated than the accepted answer and doesn't do an in-place sort.
there are many ways to achieve the same, I have given different thought process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.