0

i would like to sort by postal address but i am unable to i have seen some Linq functions tried them but i can't seem to get all the required parameters needed. for example i saw this one example

list.Sort((p, q) => p.Category.CompareTo(q.Category)); /*has and error that says  cannot convert lamba expressions to type '|Comparer' because it is not a delegate*/

but i dont seem to understand how to use it.

MyCustomList.cs

  class MyCustomList
    {
        private string name;
        private string postalAddress;


    public MyCustomList(string name, string postalAddress)
        {
            this.name = name;
            this.postalAddress = postalAddress;
        }

//getters and setters
   public string Name
        {
            get
            {
                return name;
            }

            set
            {
                name = value;
            }
        }

        public string PostalAddress
        {
            get
            {
                return postalAddress;
            }

            set
            {
                postalAddress = value;
            }
        }
}

Form1.cs

 ArrayList list = new ArrayList();
 list.Add(new MyCustomList("A somename","A Fake Postal Address");
 list.Add(new MyCustomList("B somename","B Fake Postal Address");

list.Sort(); // Sort by Postal adress
5
  • you've called sort with a lower case 's' - so this wont compile. Assuming that's a mistake in the question. why haven't you tried what the example says list.Sort((a,b) => a.PostalAddress.CompareTo(b.PostalAddress)) ? Commented Dec 4, 2019 at 19:55
  • it gives an error that says, cannot convert lamba expressions to type '|Comparer' because it is not a delegate type Commented Dec 4, 2019 at 19:58
  • the method you are trying to use is static, you need to to call Array.Sort(list,(a,b) => a.PostalAddress.CompareTo(b.PostalAddress)) Commented Dec 4, 2019 at 20:04
  • have you tried this:stackoverflow.com/questions/32235684/… ? Commented Dec 4, 2019 at 20:16
  • Verified solution please check this stackoverflow.com/a/57371579/6923146 Commented Jan 7, 2020 at 4:29

3 Answers 3

2

Do you really need to use ArrayList?

It's a relic from the pre-generics days of .NET, and you should really be using an implementation of IEnumerable<T> where possible e.g. List<T>.

LINQ operates on IEnumerable<T>, so won't work with your ArrayList, and the method you are looking for is OrderBy or OrderByDescending.

Example:

var list = new List<MyCustomList>();
list.Add(new MyCustomList("A somename","A Fake Postal Address"));
list.Add(new MyCustomList("B somename","B Fake Postal Address"));

list.OrderBy(cl => cl.Postcode); // Sort by Postal address
Sign up to request clarification or add additional context in comments.

Comments

0

First stop using ArrayList - its as good as obsolete.

Either using Array like this

var list = MyCustomList[2];
list[0] = new MyCustomList(...
list[1] = new MyCustomList(....

or use something like the List<T> class

var list = new List<MyCustomList>();
list.Add(new MyCustomList(...
list.Add(new MyCustomList(...

If you use array then the Sort function that takes an instance of Comparison<T> is static

see the documentation here https://learn.microsoft.com/en-us/dotnet/api/system.array.sort?view=netframework-4.8#System_Array_Sort__1___0___System_Comparison___0__

you need to call it like so:

Array.Sort(list, (a,b) => a.PostalAddress.CompareTo(b.PostalAddress))

or use linq on your List or Array and use OrderBy

var orderedList = list.OrderBy(a => a.PostalAddress);

4 Comments

i get two errors when i do this 1 .argument 1: cannot convert System.Collections.arraylist to system.array 2 .cannot convert lambda expression to type array because it is not a delegate type
sorry my apologies. I hadn't clicked you were used ArrayList ... why are you using ArrayList???
i am not really sure but i have used it throughout the program i think its too late for me to change because i would need to change it through out my software :( please tell me there is a walk around for array list
i have changed it to list since array list doesn't really support lamba functions just not sure what is the speedyness of the sorting if it has more than 25 thousand data well anyways thank for the assist :)
0

Already approved by many https://stackoverflow.com/a/57371579/6923146

For order wise sorting with a specific field in c# using linq

list = list.OrderByDescending(x => x.Name).ToList();
list = list.OrderBy(x => x.Name).ToList();
//list.OrderBy(x => x.YourClassSpecificField).ToList()

Example:

please try to run following code in fiddle :

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        List<MyCustomList> list = new List<MyCustomList>();

            list.Add(new MyCustomList("A somename", "A Fake Postal Address"));
            list.Add(new MyCustomList("B somename", "B Fake Postal Address"));

            //list.Sort();
            Console.WriteLine("descending order");
            list = list.OrderByDescending(x => x.Name).ToList();
            foreach (MyCustomList o in list)
            {
                Console.WriteLine(o.Name + " -- " + o.PostalAddress );
            }
            Console.WriteLine("ascending order");
            list = list.OrderBy(x => x.Name).ToList();
            foreach (MyCustomList o in list)
            {
                Console.WriteLine(o.Name + " -- " + o.PostalAddress );
            }
    }

    public class MyCustomList
    {
        private string name;
        private string postalAddress;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public string PostalAddress
        {
            get { return postalAddress; }
            set { postalAddress = value; }
        }
        public MyCustomList(string name, string postalAddress)
        {
            this.name = name;
            this.postalAddress = postalAddress;
        }
    }

}

Comments

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.