110,525
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] array = { 3, 4, 7, 2, 1, 8, 9, 10 };
array.BubbleSort(); //按照数值排序
Console.WriteLine(String.Join(", ", array));
array.BubbleSort((x, y) => y - x); //按照数值反向排序
Console.WriteLine(String.Join(", ", array));
array.BubbleSort((x, y) => x.ToString().CompareTo(y.ToString())); //按照字面排序(10排在2前面)
Console.WriteLine(String.Join(", ", array));
}
}
static class SortHelper
{
public static void BubbleSort<TSource>(this TSource[] source, Func<TSource, TSource, int> compareto = null)
{
if (compareto == null)
{
if (typeof(TSource).GetInterface(typeof(IComparable).ToString(), false) != null)
compareto = (x, y) => (x as IComparable).CompareTo(y);
else
compareto = (x, y) => x.GetHashCode() - y.GetHashCode();
}
for (int i = 0; i <= source.Count(); i++)
{
for (int j = 1; j < source.Count() - i; j++)
{
if (compareto(source[j], source[j - 1]) < 0)
{
TSource temp = source[j - 1];
source[j - 1] = source[j];
source[j] = temp;
}
}
}
}
}
}
public delegate void BuyTicketDelegate(string name);
class Program
{
public delegate bool SortDelegate(object objA,object objB);
static void Main(string[] args)
{
Student[] stu = { new Student(2, 3.2m), new Student(1, 4.1m), new Student(5, 1.5m), new Student(4, 5.4m), new Student(3, 2.3m) };
Sort(stu, Student.intMehod);
foreach (Student s in stu)
Console.WriteLine(s.ToString());
Console.WriteLine("==================");
Sort(stu, Student.decimalMehod);
foreach (Student s in stu)
Console.WriteLine(s.ToString());
Console.ReadLine();
}
static void Sort(object[] obj,SortDelegate sortMethod)
{
for (int i = 0; i < obj.Length; i++)
{
for (int j = i; j < obj.Length; j++)
{
if (sortMethod(obj[i], obj[j]))
{
object temp = obj[i];
obj[i] = obj[j];
obj[j] = temp;
}
}
}
}
}
public class Student
{
public int id;
public decimal juankuan;
public Student(int id, decimal juankuan)
{
this.id = id;
this.juankuan = juankuan;
}
public static bool intMehod(object objA, object objB)
{
return (objA as Student).id > (objB as Student).id;
}
public static bool decimalMehod(object objA, object objB)
{
return (objA as Student).juankuan > (objB as Student).juankuan;
}
public override string ToString()
{
return this.id + "====" + this.juankuan;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class MyComparer<TSource, TResult> : IComparable
{
public TSource source;
public Func<TSource, TResult> Selector;
public Func<TResult, TResult, int> Compareto;
public int CompareTo(object other)
{
return Compareto(Selector(source), Selector(((MyComparer<TSource, TResult>)other).source));
}
public MyComparer(TSource obj, Func<TSource, TResult> selector, Func<TResult, TResult, int> compareto)
{
source = obj;
Selector = selector;
Compareto = compareto;
}
}
class Program
{
static void Main(string[] args)
{
int[] array = { 3, 4, 7, 2, 1, 8, 9, 10 };
BubbleSort(array, x => x.ToString(), (x, y) => x.CompareTo(y));
Console.WriteLine(String.Join(", ", array));
BubbleSort(array, x => x, (x, y) => x - y);
Console.WriteLine(String.Join(", ", array));
}
static void BubbleSort<TSource, TResult>(TSource[] source, Func<TSource, TResult> selector, Func<TResult, TResult, int> compareto)
{
for (int i = 0; i <= source.Count(); i++)
{
for (int j = 1; j < source.Count() - i; j++)
{
if (new MyComparer<TSource, TResult>(source[j], selector, compareto).CompareTo(new MyComparer<TSource, TResult>(source[j - 1], selector, compareto)) < 0)
{
TSource temp = source[j - 1];
source[j - 1] = source[j];
source[j] = temp;
}
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class MyComparer<TSource> : IComparable
{
public TSource source;
public Func<TSource, IComparable> selector = x => x as IComparable;
public int CompareTo(object other)
{
return (selector(source) as IComparable).CompareTo(selector(((MyComparer<TSource>)other).source));
}
public MyComparer(TSource obj)
{
source = obj;
}
}
class Program
{
static void Main(string[] args)
{
int[] array = { 3, 4, 7, 2, 1, 8, 9 };
BubbleSort(array);
Console.WriteLine(String.Join(", ", array));
}
static void BubbleSort<T>(T[] source)
{
for (int i = 0; i <= source.Count(); i++)
{
for (int j = 1; j < source.Count() - i; j++)
{
if (new MyComparer<T>(source[j]).CompareTo(new MyComparer<T>(source[j - 1])) < 0)
{
T temp = source[j - 1];
source[j - 1] = source[j];
source[j] = temp;
}
}
}
}
}
}
public static bool operator <(Test<T> a, Test<T> b)
{
if (a is IComparable)
return ((IComparable)a).CompareTo(b) < 0;
else
return false;
}
public static bool operator <(Test<T> a, Test<T> b)
{
if (typeof(IComparable).IsAssignableFrom(typeof(T)))
return ((IComparable)a).CompareTo(b) < 0;
else
return ..........你自己的代码;
}