当前位置:网站首页>Array sort 2
Array sort 2
2022-07-26 04:29:00 【VB.Net】
Copyright notice : This article is an original blog article , For reprint, please indicate the source of this article and the author's network name in a prominent position , May not be used for commercial purposes without the permission of the author .
Sort for classes , First of all, we should realize IComparable Interface . It defines a general type specific comparison method , Value types or classes sort their instances by implementing this method .
The following is a student class , Only student ID is included id(Integer) And name name(String). Ensure that this class implements IComparable Interface , And implemented CompareTo Method :
Public Class student
Implements IComparable(Of student)
Public Function CompareTo(other As student) As Integer Implements IComparable(Of student).CompareTo
……
End Function
The specific code is as follows
Public Class student
Implements IComparable(Of student)
Public Property id As Integer
Public Property Name As String
Sub New(ByVal id As Integer, ByVal Name As String)
Me.id = id
Me.Name = Name
End Sub
Public Function CompareTo(other As student) As Integer Implements IComparable(Of student).CompareTo
Return id.CompareTo(other.id)
End Function
Public Overrides Function ToString() As String
Return id & ":" & Name
End Function
End Class The code uses Integer Of CompareTo Method .
Because it involves the following output , It also rewrites ToString Method , according to “ Student number : full name ” Mode output of .
Sub Main Use in Student The code for the class is as follows :
Dim teststudent() As student = New student() {
New student(12, "Bolger"),
New student(13, "Quin"),
New student(11, "Anie"),
New student(10, "Besterman"),
New student(14, "Orvis")
}
Console.WriteLine(" Sort by student number :")
Dim copystudent1() As student
copystudent1 = teststudent.Clone
Array.Sort(copystudent1)
For i As Integer = 0 To copystudent1.Length - 1
Console.WriteLine(copystudent1(i).ToString & " ")
Next
Console.WriteLine()
Console.WriteLine(" In reverse order of student number :")
Dim copystudent2() As student
copystudent2 = teststudent.Clone
Array.Sort(copystudent2)
Array.Reverse(copystudent2)
For i As Integer = 0 To copystudent2.Length - 1
Console.WriteLine(copystudent2(i).ToString & " ")
Next
Console.WriteLine()
Console.WriteLine(" Original array :")
Dim copystudent3() As student
copystudent3 = teststudent.Clone
Array.Sort(copystudent3, AddressOf Comparison)
For i As Integer = 0 To copystudent3.Length - 1
Console.WriteLine(copystudent3(i).ToString & " ")
Next
Console.WriteLine()
Console.ReadKey() Run as shown in the figure below :
In the above code, the implementation of id Sort , What if you sort by name ?
Do not add code in the class , Add the following code to the module , Use names for comparison :
Private Function Comparison(x As student, y As student) As Integer
Return (x.Name).CompareTo(y.Name)
End FunctionSub Main Add code to :
Console.WriteLine(" Sort by name :")
Dim copystudent3() As student
copystudent3 = teststudent.Clone
Array.Sort(copystudent3, AddressOf Comparison)
For i As Integer = 0 To copystudent3.Length - 1
Console.WriteLine(copystudent3(i).ToString & " ")
Next
Console.WriteLine()
Console.ReadKey()
Exit SubThe operation results are as follows :

What we use here is Array Of Public Shared Sub Sort(Of T) ( array As T(), comparison As Comparison(Of T) ) Method , Using the Comparison(Of T) entrust , Use the following statement :
Public Delegate Function Comparison(Of In T) ( _
x As T, _
y As T _
) As Integer
The simplified method of use is directly in Sort() Method used in Addressof Method name .
If the return value of this delegate method is less than 0, that x be ranked at y front . The above code is simple to use String Of CompareTo Method .
because .net Under the platform C# and vb.NET Very similar , This article can also be C# Provide reference for enthusiasts .
Learn more vb.net knowledge , Please see vb.net course Catalog
边栏推荐
- 一、基础入门
- 综合评价与决策方法
- Use of anonymous functions
- What are the consequences and problems of computer system restoration
- How to write the summary? (including 7 easily ignored precautions and a summary of common sentence patterns in 80% of review articles)
- How to write abstract in English thesis?
- The auxiliary role of rational cognitive educational robot in teaching and entertainment
- UE4 键盘控制开关灯
- Threadpooltaskexecutor and ThreadPoolExecutor
- Life related -- the heartfelt words of a graduate tutor of Huake (mainly applicable to science and Engineering)
猜你喜欢

Steam science education endows classroom teaching with creativity

吴恩达机器学习课后习题——线性回归

qt编译报错整理及Remote模块下载

Spark Structured Streaming HelloWorld

生活相关——十年的职业历程(转)

Keil V5 installation and use

A series of problems about the number of DP paths

How to download the supplementary literature?

Analyzing the curriculum design evaluation system of steam Education

Several methods of realizing high-low byte or high-low word exchange in TIA botu s7-1200
随机推荐
Credit card fraud detection based on machine learning
Yadi started to slow down after high-end
makefile知识再整理(超详细)
旋转数组最小数字
Which websites can I visit to check the latest medical literature?
2、 Internationally renowned project HelloWorld
Authentication Encyclopedia (cookies, sessions, tokens, JWT, single sign on), in-depth understanding and understanding of authentication
人脸数据库收集总结
Recommendation | DBT skills training manual: baby, you are the reason why you live
Acwing_ 12. Find a specific solution for the knapsack problem_ dp
What model is good for the analysis of influencing factors?
吴恩达机器学习课后习题——逻辑回归
生活相关——减少期待,更快乐
A series of problems about the number of DP paths
RTSP/Onvif协议视频平台EasyNVR服务一键升级功能的使用教程
Compiled by egg serialize JS
1. If function of Excel
再获认可 | 赛宁网安连续上榜《CCSIP 2022中国网络安全产业全景图》
What are the consequences and problems of computer system restoration
五、域对象共享数据