06 June 2016

Sorting Arrays [C#]

Sorting array of primitive types
Sort int array
Sort Array C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SortIntArray
{

    class Program
    {
 
        static void Main(string[] args)
        {
            //Sort int array
            int[] numbers = { 8, 2, 6, 9, 1 };
            Array.Sort(numbers);
            foreach(int i in numbers){
                Console.WriteLine(i);
            }
            Console.Read();
        }
    }
}
Sort string array
Sort Array C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SortStringArray
{

    class Program
    {
 
        static void Main(string[] args)
        {
            //Sort string array
            string[] numbers = { "Z","E","L","A","D" };
            Array.Sort(numbers);
            foreach(string i in numbers){
                Console.WriteLine(i);
            }
            Console.Read();
        }
    }
}

Sorting array of custom type using delegate

Sort array by age delegate
Sort Array C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SortDelegateDemo
{
    class User
    {
        public string name { set; get; }
        public int age { set; get; }
        public User(string Name, int Age)
        {
            name = Name;
            age = Age;
        }
    }
    class Program
    {

        public static void Main(string[] args)
        {
            User[] user = new User[3] { new User("Y", 23),  // name, age
                             new User("B", 20),
                             new User("A", 25) };

            Array.Sort(user, delegate(User a, User b)
            {
                return a.age.CompareTo(b.age);
            });

            foreach (User u in user)
            {
                Console.WriteLine("Name: " + u.name + " Age: " + u.age);
            }
            Console.Read();
        }
    }
}

Sort array by name delegate
Sort Array C# 2016
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SortDelegateDemo
{
    class User
    {
        public string name { set; get; }
        public int age { set; get; }
        public User(string Name, int Age)
        {
            name = Name;
            age = Age;
        }
    }
    class Program
    {

        public static void Main(string[] args)
        {
            User[] user = new User[3] { new User("Y", 23),  // name, age
                             new User("B", 20),
                             new User("A", 25) };

            Array.Sort(user, delegate(User a, User b)
            {
                return a.name.CompareTo(b.name);
            });

            foreach (User u in user)
            {
                Console.WriteLine("Name: " + u.name + " Age: " + u.age);
            }
            Console.Read();
        }
    }
}





0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang