当前位置:网站首页>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
边栏推荐
- What are the duplicate check rules for English papers?
- What if win11 cannot wake up from sleep? Solution of win11 unable to wake up during sleep
- Creative design principle of youth maker Education
- Chapter 3 how to use sourcetree to submit code
- 一个sql server查询截止某个日期最新的记录
- Design and implementation of smart campus applet based on cloud development
- Yadi started to slow down after high-end
- egg-sequelize JS编写
- Dijango learning
- How does win11 set the theme color of the status bar? Win11 method of setting theme color of status bar
猜你喜欢

1. Mx6u system migration-6-uboot graphical configuration

Use Baidu PaddlePaddle easydl to complete garbage classification

Unable to find sygwin.s file during vscode debugging

一个sql server查询截止某个日期最新的记录

Support proxy direct connection to Oracle database, jumpserver fortress v2.24.0 release

RTSP/Onvif协议视频平台EasyNVR服务一键升级功能的使用教程

I.MX6U-ALPHA开发板(主频和时钟配置实验)

UE4 获取玩家控制权的两种方式

UE4 靠近物体时显示文字,远离时文字消失

性能和成本的综合架构:单元化架构
随机推荐
建设面向青少年的创客教育实验室
人脸数据库收集总结
Dijango learning
[C language foundation] 13 preprocessor
Solution: runtimeerror: expected object of scalar type int but got scalar type double
When you try to delete all bad code in the program | daily anecdotes
How to write the summary? (including 7 easily ignored precautions and a summary of common sentence patterns in 80% of review articles)
dijikstra(先预处理)+dfs,relocation truncated to fit
图论:拓扑排序
Rotate array minimum number
Li Kou daily question - day 42 -661. Picture smoother
Segger embedded studio cannot find xxx.c or xxx.h file
Acwing_ 12. Find a specific solution for the knapsack problem_ dp
Postman imports curl, exports curl, and exports corresponding language codes
Page pull-up refresh and pull-down loading
5、 Domain objects share data
Day24 job
What are the consequences and problems of computer system restoration
makefile知识再整理(超详细)
View and modify the number of database connections