排序是我們編程時(shí)的常用操作,實(shí)現(xiàn)方式也有很多種,本篇文章列舉幾種我常用的用法,希望對(duì)大家有用!
數(shù)組排序
最常見的排序是對(duì)一個(gè)數(shù)組排序,比如:
int[] aArray = new int[8] { 18, 17, 21, 23, 11, 31, 27, 38 };
第一種:我們最熟悉的冒泡排序法:即每個(gè)值都和它后面的數(shù)值比較,每次拿出最小值
static void Main(string[] args)
{
int[] aArray = new int[8] { 18, 17, 21, 23, 11, 31, 27, 38 };
for (int j = 0; j < aArray.Length - 1; j++)
{
for (int i = 0; i < aArray.Length - 1; i++)
{
if (aArray[i] > aArray[i + 1])
{
int temp = aArray[i];
aArray[i] = aArray[i + 1];
aArray[i + 1] = temp;
}
}
}
foreach (var a in aArray)
{
Console.Write($"{a} ");
}
Console.ReadKey();
}
運(yùn)行結(jié)果:
第二種:利用Array.Sort排序:
① 升序排列:
static void Main(string[] args)
{
int[] aArray = new int[8] { 18, 17, 21, 23, 11, 31, 27, 38 };
Array.Sort(aArray);
foreach (var a in aArray)
{
Console.Write($"{a} ");
}
Console.ReadKey();
}
運(yùn)行結(jié)果:
② 降序排列:先升序排列,然后對(duì)數(shù)組反轉(zhuǎn)
static void Main(string[] args)
{
int[] aArray = new int[8] { 18, 17, 21, 23, 11, 31, 27, 38 };
Array.Sort(aArray);
Array.Reverse(aArray);
foreach (var a in aArray)
{
Console.Write($"{a} ");
}
Console.ReadKey();
}
運(yùn)行結(jié)果:
02
—
List排序
大多數(shù)時(shí)候,我們需要將list集合里面的數(shù)據(jù)進(jìn)行排序,
① 如果list直接放置的數(shù)值類型的數(shù)據(jù)就比較簡單,比如:
List<int> aList = new List<int> { 18, 17, 21, 23, 11, 31, 27, 38 };
方法一:利用List<T> 自帶的Sort排序方法
升序排列:
降序排列:
aList.Sort();
aList.Reverse();
sort也可以用如下方式操作:
list.Sort((a, b) => a.CompareTo(b));//升序
list.Sort((a, b) => b.CompareTo(a));//降序
方法二:利用List的OrderBy與OrderByDescending方法
升序排列:
List<int> aList = new List<int> { 18, 17, 21, 23, 11, 31, 27, 38 };
aList = aList.OrderBy(a => a).ToList();
降序排列:
List<int> aList = new List<int> { 18, 17, 21, 23, 11, 31, 27, 38 };
aList = aList.OrderByDescending(a => a).ToList();
方法三:利用link,這種感覺和方法二是一回事
List<int> aList = new List<int> { 18, 17, 21, 23, 11, 31, 27, 38 };
aList = (from a in aList orderby a ascending select a).ToList();//升序
aList = (from a in aList orderby a descending select a).ToList();//降序
②如果list存放的是一個(gè)類型,比如:
方法一: 一個(gè)集合存放學(xué)生的信息,按照學(xué)生的成績升序排列:這里列舉了2種方法,方法一注釋了
static void Main(string[] args)
{
List<Student> stuList = new List<Student>
{
new Student() {name = "zyr", age = 23, score = 99},
new Student() {name = "zls", age = 25, score = 95},
new Student() {name = "zsq", age = 27, score = 100},
new Student() {name = "zlw", age = 15, score = 69},
new Student() {name = "ywe", age = 17, score = 72},
new Student() {name = "asw", age = 29, score = 58}
};
//方法1 升序
//stuList.Sort((x, y) => x.score.CompareTo(y.score));
//方法2 升序
stuList = stuList.OrderBy(stu=>stu.score).ToList();
foreach (var stu in stuList)
{
Console.WriteLine($"{stu}");
}
Console.ReadKey();
}
public class Student
{
public string name { get; set; }
public int age { get; set; }
public int score { get; set; }
public override string ToString()
{
return $"姓名是:{name},年齡是:{age},得分是:{score}";
}
}
運(yùn)行結(jié)果:
方法二: 加入這個(gè)學(xué)生成績有重復(fù)的,對(duì)于重復(fù)的成績按照年齡再排序:這里列舉了2種方法,方法一注釋了
static void Main(string[] args)
{
List<Student> stuList = new List<Student>
{
new Student() {name = "zyr", age = 23, score = 99},
new Student() {name = "zls", age = 25, score = 95},
new Student() {name = "zls", age = 22, score = 95},
new Student() {name = "zsq", age = 27, score = 100},
new Student() {name = "zlw", age = 15, score = 69},
new Student() {name = "ywe", age = 17, score = 72},
new Student() {name = "asw", age = 29, score = 58},
new Student() {name = "ywe", age = 18, score = 72},
new Student() {name = "zsq", age = 16, score = 100},
};
//方法1 升序
//stuList.Sort((x, y) =>
//{
// int ret = x.score.CompareTo(y.score);
// if (ret == 0)
// {
// return x.age.CompareTo(y.age);
// }
// else
// {
// return ret;
// }
//});
//方法2 升序
//stuList = stuList.OrderBy(stu=>stu.score).ThenBy(stu => stu.age).ToList();
foreach (var stu in stuList)
{
Console.WriteLine($"{stu}");
}
Console.ReadKey();
}
public class Student
{
public string name { get; set; }
public int age { get; set; }
public int score { get; set; }
public override string ToString()
{
return $"姓名是:{name},年齡是:{age},得分是:{score}";
}
}
該文章在 2024/3/19 10:35:08 編輯過