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
  • 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)) ? – Dave Dec 04 '19 at 19:55
  • it gives an error that says, cannot convert lamba expressions to type '|Comparer' because it is not a delegate type – bread holding Dec 04 '19 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)) – Dave Dec 04 '19 at 20:04
  • have you tried this:https://stackoverflow.com/questions/32235684/sort-arraylist-with-two-dimensional-objects ? – Nima Habibollahi Dec 04 '19 at 20:16
  • Verified solution please check this https://stackoverflow.com/a/57371579/6923146 – Hardik Masalawala Jan 07 '20 at 04:29

3 Answers3

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
Johnathan Barclay
  • 18,599
  • 1
  • 22
  • 35
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);
Dave
  • 2,829
  • 3
  • 17
  • 44
  • 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` – bread holding Dec 04 '19 at 20:13
  • sorry my apologies. I hadn't clicked you were used ArrayList ... why are you using ArrayList??? – Dave Dec 04 '19 at 20:17
  • 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 – bread holding Dec 04 '19 at 20:21
  • 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 :) – bread holding Dec 04 '19 at 20:32
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;
        }
    }

}