vb.net实现一个自定义类数组的排序
vb.net实现一个自定义类数组的排序 今天下午做东西时遇到这样的问题:
把公司所有员工的业绩(回访数量,收费情况)用一个柱型图表示出来,要求:
A:Y轴表示公司的所有员工且业绩为0的员工不显示出来,X轴表示业绩数量(回访数量+收费情况);
B:按要求对柱图实现升序或着降序
按以上要求所得思路为:
首先建立一个类(也可以通过结构来实现),类中包含的属性(或字段)为员工ID属性(或字段),员工回访数量属性(或字段),员工收费情况属性(或字段),员工业绩属性等等。(为了能利用Array中的sort需要实现一个接口,会在原码中说明)
接着通过一个table来通过颜色或着图片表现出来。下面是实现的原码:
Public Class wordload
Implements IComparable'一定要实现这个接口,才能利用下面的:Array.Sort
Public pay As Integer '维护数量
Public go_back As Integer '回访数量
Public name As String '用户名称
Public total_ As Integer '维护数量+回访数量
'以下是想通过那个属性(或字段),来进行排序
Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
Dim other As wordload = CType(obj, wordload)
Return total_.CompareTo(other.total_)
End Function
Public Sub New(ByVal pay_value As Integer, ByVal go_back_value As Integer, ByVal name_value As String)
pay = pay_value
name = name_value
go_back = go_back_value
total_ = pay + go_back
End Sub
End Class
Public Sub creat_test(ByVal myarr() As wordload, ByVal mytable As System.Web.UI.WebControls.Table)
Array.Sort(myarr)'说明利用Array进行排序
Dim rows_num As Integer = myarr.Length + 1 '说明:人员做为y轴,即人员的个数做为行数,其中为了写表头,要加一行,所以表的总行数为,人员个数+1
Dim total_comm_num As Integer = myarr(myarr.Length - 1).total_ '说明:列数为工作量加1,因为人员的名字要占一列
Dim pay_width As Integer '定义维护宽度
Dim go_back_width As Integer '定义回访宽度
Dim myimage As HtmlControls.HtmlImage
Dim myspan As HtmlControls.HtmlGenericControl
Dim begin_myrow As New Web.UI.WebControls.TableRow
Dim mycell1 As New UI.WebControls.TableCell
Dim mycol1 As New Color
mycell1.Text = '员工'
mycell1.BackColor = mycol1.Green
mycell1.Style.Add('BACKGROUND-COLOR', '#949294')
mycell1.Style.Add('FONT-WEIGHT', 'bold')
mycell1.Style.Add('FONT-SIZE', '18pt')
mycell1.Style.Add('COLOR', '#ffffff')
mycell1.Width = WebControls.Unit.Pixel(60)
begin_myrow.Cells.Add(mycell1)
begin_myrow.Width = Unit.Pixel(700)
For b As Integer = 0 To 7 '写第一行的列
Dim mycol As New Color
If b = 0 Then
Dim mycell As New UI.WebControls.TableCell
'mycell.BackColor = mycol.LightGreen
begin_myrow.Cells.Add(mycell)
Else
myspan = New HtmlControls.HtmlGenericControl
myspan.InnerText = ((total_comm_num * (b - 1) / 7) \ 1).ToString
myspan.Style.Add('WIDTH', '80px') 'WIDTH: 100px;
myspan.Style.Add('HEIGHT', '40px') ' HEIGHT: 100px;: bold; FONT-SIZE: 18pt;COLOR: #ffffff;
myspan.Style.Add('BACKGROUND-COLOR', '#949294')
myspan.Style.Add('FONT-WEIGHT', 'bold')
myspan.Style.Add('FONT-SIZE', '18pt')
myspan.Style.Add('COLOR', '#ffffff')
begin_myrow.Cells(1).Controls.Add(myspan)
End If
Next
mytable.Rows.Add(begin_myrow)
For i As Integer = 0 To rows_num - 2
pay_width = (500 * myarr(i).pay / total_comm_num) \ 1 '给维护宽度进行赋值
go_back_width = (500 * myarr(i).go_back / total_comm_num) \ 1 '给回访宽度进行赋值
Dim myrow As New Web.UI.WebControls.TableRow
myrow.Width = Unit.Pixel(700)
Dim j As Integer
'以下循环是添加维护情况
For j = 0 To pay_width
If j = 0 Then '在第一列要写进用户的名称
Dim mycell As New UI.WebControls.TableCell
mycell.Text = myarr(i).name
mycell.Width = WebControls.Unit.Pixel(60)
Dim mycol As New Color
mycell.BorderColor = mycol.DeepPink
myrow.Cells.Add(mycell)
Dim mycell2 As New UI.WebControls.TableCell
myrow.Cells.Add(mycell2)
Else '在第二列添加图片信息
myspan = New HtmlControls.HtmlGenericControl
myimage = New HtmlControls.HtmlImage
myimage.Src = 'Images/navbar.gif' '要显示的中间图片
myspan.Controls.Add(myimage)
myrow.Cells(1).Controls.Add(myspan)
End If
Next
'以下循环是添加回访情况
For j = 0 To go_back_width
myspan = New HtmlControls.HtmlGenericControl
myimage = New HtmlControls.HtmlImage
myimage.Src = 'Images/navbar2.gif' '要显示的中间图片
myspan.Controls.Add(myimage)
myrow.Cells(1).Controls.Add(myspan)
Next
mytable.Rows.Add(myrow)
Next
mytable.Width = Unit.Pixel(700)
End Sub
后记:以上代码在win2000+vs.net 中运行通过,由于是刚刚测试完成,代码有点乱(变量名称也是随意的,有点不能代表意思!)