0
点赞
收藏
分享

微信扫一扫

【c#编程技术总结】c#List排序补充


欢迎加入Unity业内qq交流群:956187480


 //方法一sort排序使用lambda表达式

List<int> list = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

list.Sort((x, y) => -x.CompareTo(y));//降序
list.Sort((x, y) => x.CompareTo(y));//升序

  //方法二简单sort排序

List<int> list = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
list.Reverse();// 反转顺序
list.Sort();// 升序排序

 //方法三复杂对象

List<Student> list = new List<Student>();
list.Sort(
delegate (Student p1, Student p2)
{
return p1.sno.CompareTo(p2.sno);//升序
//return p1.sno == p1.sno ? 0 : (p1.sno > p1.sno) ? 1 : -1;
});
//list.Sort((x, y) => { return x.sno.CompareTo(y.sno); });

 方法四OrdeOrderBy运用

Debug.Log("****顺序排列****");
var tlist = list.OrderBy(t => t.sno).ToList();

Debug.Log("****倒序排列****");
var tlist = list.OrderByDescending(t => t.sno).ToList();

方法五 chon重写Comparable

public class Student: IComparable<Student>
{
public int sno;
public string name;

public Student(int sno, string name)
{
this.sno = sno;
this.name = name;
}

//重写的CompareTo方法,根据Id排序
public int CompareTo(Student other)
{
if (null == other)
{
return 1;//空值比较大,返回1
}
//return this.Id.CompareTo(other.Id);//升序
return other.sno.CompareTo(this.sno);//降序
}
}

或者

public int Compare(Student x, Student y)
{
return x.sno.CompareTo(y.sno);//升序
}

测试脚本如下

#region 模块信息
// **********************************************************************
// Copyright (C) 2019 Blazors
// Please contact me if you have any questions
// File Name: Test
// Author:
// WeChat||QQ:
// **********************************************************************
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class Student: IComparable<Student>
{
public int sno;
public string name;

public Student(int sno, string name)
{
this.sno = sno;
this.name = name;
}

//重写的CompareTo方法,根据Id排序
public int CompareTo(Student other)
{
if (null == other)
{
return 1;//空值比较大,返回1
}
//return this.Id.CompareTo(other.Id);//升序
return other.sno.CompareTo(this.sno);//降序
}
public int Compare(Student x, Student y)
{
return x.sno.CompareTo(y.sno);//升序
}

}
public class Test : MonoBehaviour
{
List<Student> targetList;
// Use this for initialization
void Start()
{

}
private void Update()
{
//方法一
if (Input.GetKeyDown(KeyCode.E))//sort排序使用lambda表达式
{
List<int> list = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

list.Sort((x, y) => -x.CompareTo(y));//降序
list.Sort((x, y) => x.CompareTo(y));//升序


}
//方法二
if (Input.GetKeyDown(KeyCode.W))//简单sort排序
{
List<int> list = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
list.Reverse();// 反转顺序
list.Sort();// 升序排序

}
//方法三
if (Input.GetKeyDown(KeyCode.W))//简单sort排序
{
List<Student> list = new List<Student>();
list.Sort(
delegate (Student p1, Student p2)
{
return p1.sno.CompareTo(p2.sno);

});
//list.Sort((x, y) => { return x.sno.CompareTo(y.sno); });
}

//方法三
if (Input.GetKeyDown(KeyCode.Q))//OrderBy的运用
{
targetList = new List<Student>();
for (int i = 0; i < 10; i++)
{
targetList.Add(new Student(i, "小明" + i));
}
var tList01 = OutOfOrder(targetList);
var tList02 = InOrder(tList01);
var tList03 = OutOfOrder(tList02);
InvertedOrder(tList03);
}

}
private List<Student> InOrder(List<Student> list)
{
Debug.Log("****顺序排列****");
var tlist = list.OrderBy(t => t.sno).ToList();
string str = ""; ;
foreach (var item in tlist)
{
str += item.sno;
}
Debug.Log("顺序后学号:" + str);
return tlist;
}
private List<Student> InvertedOrder(List<Student> list)
{
Debug.Log("****倒序排列****");
var tlist = list.OrderByDescending(t => t.sno).ToList();
string str = ""; ;
foreach (var item in tlist)
{
str += item.sno;
}
Debug.Log("倒序后学号:" + str);
return tlist;
}


/// <summary>
/// List乱序
/// </summary>
/// <param name="a"></param>
/// <returns></returns>
public List<Student> OutOfOrder(List<Student> a)
{
Debug.LogError("****打乱列表****");
List<Student> b = new List<Student>();
int countNum = a.Count;
//使用while循环,保证将a中的全部元素转移到b中而不产生遗漏
while (b.Count < countNum)
{
//随机将a中序号为index的元素作为b中的第一个元素放入b中
int index = UnityEngine.Random.Range(0, a.Count - 1);
//检测是否重复,保险起见
if (!b.Contains(a[index]))
{
//若b中还没有此元素,添加到b中
b.Add(a[index]);
//成功添加后,将此元素从a中移除,避免重复取值
a.Remove(a[index]);
}
}
string str = ""; ;
foreach (var item in b)
{
str += item.sno;
}
Debug.Log("乱序后学号:" + str);
return b;
}

}

欢迎加入Unity业内qq交流群:956187480


举报

相关推荐

0 条评论