bread holding
bread holding

Reputation: 28

sort custom array list with a specific field

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

Upvotes: 0

Views: 489

Answers (3)

Hardik Masalawala
Hardik Masalawala

Reputation: 1066

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;
        }
    }

}

Upvotes: 0

Dave
Dave

Reputation: 3017

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);

Upvotes: 0

Johnathan Barclay
Johnathan Barclay

Reputation: 20353

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

Upvotes: 2

Related Questions