2010-06-10 99 views
2

我需要对包含在多个列表框中的数据进行排序。用户将能够使用单选框在两种不同类型的排序中进行选择,默认情况下在表单加载中检查其中的一种。我已经创建了所需的IF语句,以测试该单选按钮的检查条件是否为真。但我需要一些帮助来创建自定义排序算法。自定义列表框排序

每个列表都包含相似的外观数据,每行开头的唯一区别在于前缀。例如,第一个列表框中的每一行以前缀“G30”开头,第二个列表框将为“G31”,依此类推。总共有10个列表框(前缀为G30-G39)。

第一个搜索算法必须按照前13个字符的数量顺序对行进行排序。

举例: 这是数据排序

G35:45:58:11 JG07 
G35:45:20:41 JG01 
G35:58:20:21 JG03 
G35:66:22:20 JG05 
G35:45:85:21 JG02 
G35:64:56:11 JG03 
G35:76:35:11 JG02 
G35:77:97:12 JG03 
G35:54:29:11 JG01 
G35:55:51:20 JG01 
G35:76:24:20 JG06 
G35:76:55:11 JG01 

之前的外观,这是它应该如何看分拣

G35:45:20:41 JG01 
G35:45:58:11 JG07 
G35:45:85:21 JG02 
G35:54:29:11 JG01 
G35:55:51:20 JG01 
G35:58:20:21 JG03 
G35:64:56:11 JG03 
G35:66:22:20 JG05 
G35:76:24:20 JG06 
G35:76:35:11 JG02 
G35:76:55:11 JG01 
G35:77:97:12 JG03 

,你可以看到,前缀是相同的了。所以它是按照下一对整数排序,最低排序,然后是下一对排序,然后是下一排排序,但不是“JG”之后的值。

第二排序算法将忽略前13个字符,并按照“JG”之后的值的顺序排序,最高排在第一位。

有帮助吗?那里有一些代表在为你:)

在此先感谢

编辑

namespace Jumpgate_Network_Program 
{ 
    partial class ViewForm 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 


    /// <summary> 
    /// Clean up any resources being used. 
    /// </summary> 
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
    protected override void Dispose(bool disposing) 
    { 
     if (disposing && (components != null)) 
     { 
      components.Dispose(); 
     } 
     base.Dispose(disposing); 
    } 

    #region Windows Form Designer generated code 

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 
    private void InitializeComponent() 
    { 
     this.tabconSubsectors = new System.Windows.Forms.TabControl(); 
     this.tabG30 = new System.Windows.Forms.TabPage(); 
     this.lstG30 = new System.Windows.Forms.ListBox(); 
     this.tabG31 = new System.Windows.Forms.TabPage(); 
     this.lstG31 = new System.Windows.Forms.ListBox(); 
     this.tabG32 = new System.Windows.Forms.TabPage(); 
     this.lstG32 = new System.Windows.Forms.ListBox(); 
     this.tabG33 = new System.Windows.Forms.TabPage(); 
     this.lstG33 = new System.Windows.Forms.ListBox(); 
     this.tabG34 = new System.Windows.Forms.TabPage(); 
     this.lstG34 = new System.Windows.Forms.ListBox(); 
     this.tabG35 = new System.Windows.Forms.TabPage(); 
     this.lstG35 = new System.Windows.Forms.ListBox(); 
     this.tabG36 = new System.Windows.Forms.TabPage(); 
     this.lstG36 = new System.Windows.Forms.ListBox(); 
     this.tabG37 = new System.Windows.Forms.TabPage(); 
     this.lstG37 = new System.Windows.Forms.ListBox(); 
     this.tabG38 = new System.Windows.Forms.TabPage(); 
     this.lstG38 = new System.Windows.Forms.ListBox(); 
     this.tabG39 = new System.Windows.Forms.TabPage(); 
     this.lstG39 = new System.Windows.Forms.ListBox(); 
     this.tabSearch = new System.Windows.Forms.TabPage(); 
     this.btnSearch = new System.Windows.Forms.Button(); 
     this.txtSearch = new System.Windows.Forms.TextBox(); 
     this.lstResult = new System.Windows.Forms.ListBox(); 
     this.label1 = new System.Windows.Forms.Label(); 
     this.grpOptions = new System.Windows.Forms.GroupBox(); 
     this.rbtnHighest = new System.Windows.Forms.RadioButton(); 
     this.rbtnCoord = new System.Windows.Forms.RadioButton(); 
     this.tabconSubsectors.SuspendLayout(); 
     this.tabG30.SuspendLayout(); 
     this.tabG31.SuspendLayout(); 
     this.tabG32.SuspendLayout(); 
     this.tabG33.SuspendLayout(); 
     this.tabG34.SuspendLayout(); 
     this.tabG35.SuspendLayout(); 
     this.tabG36.SuspendLayout(); 
     this.tabG37.SuspendLayout(); 
     this.tabG38.SuspendLayout(); 
     this.tabG39.SuspendLayout(); 
     this.tabSearch.SuspendLayout(); 
     this.grpOptions.SuspendLayout(); 
     this.SuspendLayout(); 
     // 
     // tabconSubsectors 
     // 
     this.tabconSubsectors.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right))); 
     this.tabconSubsectors.Controls.Add(this.tabG30); 
     this.tabconSubsectors.Controls.Add(this.tabG31); 
     this.tabconSubsectors.Controls.Add(this.tabG32); 
     this.tabconSubsectors.Controls.Add(this.tabG33); 
     this.tabconSubsectors.Controls.Add(this.tabG34); 
     this.tabconSubsectors.Controls.Add(this.tabG35); 
     this.tabconSubsectors.Controls.Add(this.tabG36); 
     this.tabconSubsectors.Controls.Add(this.tabG37); 
     this.tabconSubsectors.Controls.Add(this.tabG38); 
     this.tabconSubsectors.Controls.Add(this.tabG39); 
     this.tabconSubsectors.Controls.Add(this.tabSearch); 
     this.tabconSubsectors.Location = new System.Drawing.Point(13, 39); 
     this.tabconSubsectors.Name = "tabconSubsectors"; 
     this.tabconSubsectors.SelectedIndex = 0; 
     this.tabconSubsectors.Size = new System.Drawing.Size(282, 305); 
     this.tabconSubsectors.TabIndex = 0; 
     // 
     // tabG30 
     // 
     this.tabG30.Controls.Add(this.lstG30); 
     this.tabG30.Location = new System.Drawing.Point(4, 22); 
     this.tabG30.Name = "tabG30"; 
     this.tabG30.Padding = new System.Windows.Forms.Padding(3); 
     this.tabG30.Size = new System.Drawing.Size(274, 279); 
     this.tabG30.TabIndex = 0; 
     this.tabG30.Text = "G30"; 
     this.tabG30.UseVisualStyleBackColor = true; 
     // 
     // lstG30 
     // 
     this.lstG30.FormattingEnabled = true; 
     this.lstG30.Location = new System.Drawing.Point(7, 7); 
     this.lstG30.Name = "lstG30"; 
     this.lstG30.Size = new System.Drawing.Size(259, 264); 
     this.lstG30.TabIndex = 0; 
     // 
     // tabG31 
     // 
     this.tabG31.Controls.Add(this.lstG31); 
     this.tabG31.Location = new System.Drawing.Point(4, 22); 
     this.tabG31.Name = "tabG31"; 
     this.tabG31.Padding = new System.Windows.Forms.Padding(3); 
     this.tabG31.Size = new System.Drawing.Size(274, 279); 
     this.tabG31.TabIndex = 1; 
     this.tabG31.Text = "G31"; 
     this.tabG31.UseVisualStyleBackColor = true; 
     // 
     // lstG31 
     // 
     this.lstG31.FormattingEnabled = true; 
     this.lstG31.Location = new System.Drawing.Point(8, 7); 
     this.lstG31.Name = "lstG31"; 
     this.lstG31.Size = new System.Drawing.Size(259, 264); 
     this.lstG31.TabIndex = 1; 
     // 
     // tabG32 
     // 
     this.tabG32.Controls.Add(this.lstG32); 
     this.tabG32.Location = new System.Drawing.Point(4, 22); 
     this.tabG32.Name = "tabG32"; 
     this.tabG32.Padding = new System.Windows.Forms.Padding(3); 
     this.tabG32.Size = new System.Drawing.Size(274, 279); 
     this.tabG32.TabIndex = 2; 
     this.tabG32.Text = "G32"; 
     this.tabG32.UseVisualStyleBackColor = true; 
     // 
     // lstG32 
     // 
     this.lstG32.FormattingEnabled = true; 
     this.lstG32.Location = new System.Drawing.Point(8, 7); 
     this.lstG32.Name = "lstG32"; 
     this.lstG32.Size = new System.Drawing.Size(259, 264); 
     this.lstG32.TabIndex = 1; 
     // 
     // tabG33 
     // 
     this.tabG33.Controls.Add(this.lstG33); 
     this.tabG33.Location = new System.Drawing.Point(4, 22); 
     this.tabG33.Name = "tabG33"; 
     this.tabG33.Padding = new System.Windows.Forms.Padding(3); 
     this.tabG33.Size = new System.Drawing.Size(274, 279); 
     this.tabG33.TabIndex = 3; 
     this.tabG33.Text = "G33"; 
     this.tabG33.UseVisualStyleBackColor = true; 
     // 
     // lstG33 
     // 
     this.lstG33.FormattingEnabled = true; 
     this.lstG33.Location = new System.Drawing.Point(8, 7); 
     this.lstG33.Name = "lstG33"; 
     this.lstG33.Size = new System.Drawing.Size(259, 264); 
     this.lstG33.TabIndex = 1; 
     // 
     // tabG34 
     // 
     this.tabG34.Controls.Add(this.lstG34); 
     this.tabG34.Location = new System.Drawing.Point(4, 22); 
     this.tabG34.Name = "tabG34"; 
     this.tabG34.Padding = new System.Windows.Forms.Padding(3); 
     this.tabG34.Size = new System.Drawing.Size(274, 279); 
     this.tabG34.TabIndex = 4; 
     this.tabG34.Text = "G34"; 
     this.tabG34.UseVisualStyleBackColor = true; 
     // 
     // lstG34 
     // 
     this.lstG34.FormattingEnabled = true; 
     this.lstG34.Location = new System.Drawing.Point(8, 7); 
     this.lstG34.Name = "lstG34"; 
     this.lstG34.Size = new System.Drawing.Size(259, 264); 
     this.lstG34.TabIndex = 1; 
     // 
     // tabG35 
     // 
     this.tabG35.Controls.Add(this.lstG35); 
     this.tabG35.Location = new System.Drawing.Point(4, 22); 
     this.tabG35.Name = "tabG35"; 
     this.tabG35.Padding = new System.Windows.Forms.Padding(3); 
     this.tabG35.Size = new System.Drawing.Size(274, 279); 
     this.tabG35.TabIndex = 5; 
     this.tabG35.Text = "G35"; 
     this.tabG35.UseVisualStyleBackColor = true; 
     // 
     // lstG35 
     // 
     this.lstG35.FormattingEnabled = true; 
     this.lstG35.Location = new System.Drawing.Point(8, 7); 
     this.lstG35.Name = "lstG35"; 
     this.lstG35.Size = new System.Drawing.Size(259, 264); 
     this.lstG35.TabIndex = 1; 
     // 
     // tabG36 
     // 
     this.tabG36.Controls.Add(this.lstG36); 
     this.tabG36.Location = new System.Drawing.Point(4, 22); 
     this.tabG36.Name = "tabG36"; 
     this.tabG36.Padding = new System.Windows.Forms.Padding(3); 
     this.tabG36.Size = new System.Drawing.Size(274, 279); 
     this.tabG36.TabIndex = 6; 
     this.tabG36.Text = "G36"; 
     this.tabG36.UseVisualStyleBackColor = true; 
     // 
     // lstG36 
     // 
     this.lstG36.FormattingEnabled = true; 
     this.lstG36.Location = new System.Drawing.Point(8, 7); 
     this.lstG36.Name = "lstG36"; 
     this.lstG36.Size = new System.Drawing.Size(259, 264); 
     this.lstG36.TabIndex = 1; 
     // 
     // tabG37 
     // 
     this.tabG37.Controls.Add(this.lstG37); 
     this.tabG37.Location = new System.Drawing.Point(4, 22); 
     this.tabG37.Name = "tabG37"; 
     this.tabG37.Padding = new System.Windows.Forms.Padding(3); 
     this.tabG37.Size = new System.Drawing.Size(274, 279); 
     this.tabG37.TabIndex = 7; 
     this.tabG37.Text = "G37"; 
     this.tabG37.UseVisualStyleBackColor = true; 
     // 
     // lstG37 
     // 
     this.lstG37.FormattingEnabled = true; 
     this.lstG37.Location = new System.Drawing.Point(8, 7); 
     this.lstG37.Name = "lstG37"; 
     this.lstG37.Size = new System.Drawing.Size(259, 264); 
     this.lstG37.TabIndex = 1; 
     // 
     // tabG38 
     // 
     this.tabG38.Controls.Add(this.lstG38); 
     this.tabG38.Location = new System.Drawing.Point(4, 22); 
     this.tabG38.Name = "tabG38"; 
     this.tabG38.Padding = new System.Windows.Forms.Padding(3); 
     this.tabG38.Size = new System.Drawing.Size(274, 279); 
     this.tabG38.TabIndex = 8; 
     this.tabG38.Text = "G38"; 
     this.tabG38.UseVisualStyleBackColor = true; 
     // 
     // lstG38 
     // 
     this.lstG38.FormattingEnabled = true; 
     this.lstG38.Location = new System.Drawing.Point(8, 7); 
     this.lstG38.Name = "lstG38"; 
     this.lstG38.Size = new System.Drawing.Size(259, 264); 
     this.lstG38.TabIndex = 1; 
     // 
     // tabG39 
     // 
     this.tabG39.Controls.Add(this.lstG39); 
     this.tabG39.Location = new System.Drawing.Point(4, 22); 
     this.tabG39.Name = "tabG39"; 
     this.tabG39.Padding = new System.Windows.Forms.Padding(3); 
     this.tabG39.Size = new System.Drawing.Size(274, 279); 
     this.tabG39.TabIndex = 9; 
     this.tabG39.Text = "G39"; 
     this.tabG39.UseVisualStyleBackColor = true; 
     // 
     // lstG39 
     // 
     this.lstG39.FormattingEnabled = true; 
     this.lstG39.Location = new System.Drawing.Point(8, 7); 
     this.lstG39.Name = "lstG39"; 
     this.lstG39.Size = new System.Drawing.Size(259, 264); 
     this.lstG39.TabIndex = 1; 
     // 
     // tabSearch 
     // 
     this.tabSearch.Controls.Add(this.btnSearch); 
     this.tabSearch.Controls.Add(this.txtSearch); 
     this.tabSearch.Controls.Add(this.lstResult); 
     this.tabSearch.Location = new System.Drawing.Point(4, 22); 
     this.tabSearch.Name = "tabSearch"; 
     this.tabSearch.Padding = new System.Windows.Forms.Padding(3); 
     this.tabSearch.Size = new System.Drawing.Size(274, 279); 
     this.tabSearch.TabIndex = 10; 
     this.tabSearch.Text = "Search"; 
     this.tabSearch.UseVisualStyleBackColor = true; 
     // 
     // btnSearch 
     // 
     this.btnSearch.Location = new System.Drawing.Point(150, 47); 
     this.btnSearch.Name = "btnSearch"; 
     this.btnSearch.Size = new System.Drawing.Size(75, 19); 
     this.btnSearch.TabIndex = 3; 
     this.btnSearch.Text = "Search"; 
     this.btnSearch.UseVisualStyleBackColor = true; 
     // 
     // txtSearch 
     // 
     this.txtSearch.Location = new System.Drawing.Point(8, 46); 
     this.txtSearch.Name = "txtSearch"; 
     this.txtSearch.Size = new System.Drawing.Size(136, 20); 
     this.txtSearch.TabIndex = 2; 
     // 
     // lstResult 
     // 
     this.lstResult.FormattingEnabled = true; 
     this.lstResult.Location = new System.Drawing.Point(8, 72); 
     this.lstResult.Name = "lstResult"; 
     this.lstResult.Size = new System.Drawing.Size(259, 199); 
     this.lstResult.TabIndex = 1; 
     // 
     // label1 
     // 
     this.label1.AutoSize = true; 
     this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F); 
     this.label1.Location = new System.Drawing.Point(13, 13); 
     this.label1.Name = "label1"; 
     this.label1.Size = new System.Drawing.Size(131, 20); 
     this.label1.TabIndex = 1; 
     this.label1.Text = "Select Subsector"; 
     // 
     // grpOptions 
     // 
     this.grpOptions.Controls.Add(this.rbtnHighest); 
     this.grpOptions.Controls.Add(this.rbtnCoord); 
     this.grpOptions.Location = new System.Drawing.Point(301, 68); 
     this.grpOptions.Name = "grpOptions"; 
     this.grpOptions.Size = new System.Drawing.Size(101, 72); 
     this.grpOptions.TabIndex = 2; 
     this.grpOptions.TabStop = false; 
     this.grpOptions.Text = "Sort by"; 
     // 
     // rbtnHighest 
     // 
     this.rbtnHighest.AutoSize = true; 
     this.rbtnHighest.Location = new System.Drawing.Point(7, 44); 
     this.rbtnHighest.Name = "rbtnHighest"; 
     this.rbtnHighest.Size = new System.Drawing.Size(90, 17); 
     this.rbtnHighest.TabIndex = 1; 
     this.rbtnHighest.Text = "Highest Level"; 
     this.rbtnHighest.UseVisualStyleBackColor = true; 
     this.rbtnHighest.CheckedChanged += new System.EventHandler(this.rbtnHighest_CheckedChanged); 
     // 
     // rbtnCoord 
     // 
     this.rbtnCoord.AutoSize = true; 
     this.rbtnCoord.Checked = true; 
     this.rbtnCoord.Location = new System.Drawing.Point(7, 20); 
     this.rbtnCoord.Name = "rbtnCoord"; 
     this.rbtnCoord.Size = new System.Drawing.Size(84, 17); 
     this.rbtnCoord.TabIndex = 0; 
     this.rbtnCoord.TabStop = true; 
     this.rbtnCoord.Text = "Co-ordinates"; 
     this.rbtnCoord.UseVisualStyleBackColor = true; 
     this.rbtnCoord.CheckedChanged += new System.EventHandler(this.rbtnCoord_CheckedChanged); 
     // 
     // ViewForm 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(408, 356); 
     this.Controls.Add(this.grpOptions); 
     this.Controls.Add(this.label1); 
     this.Controls.Add(this.tabconSubsectors); 
     this.MaximizeBox = false; 
     this.Name = "ViewForm"; 
     this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 
     this.Text = "View Jumpgates - JNP"; 
     this.Load += new System.EventHandler(this.ViewForm_Load); 
     this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.ViewForm_FormClosing); 
     this.tabconSubsectors.ResumeLayout(false); 
     this.tabG30.ResumeLayout(false); 
     this.tabG31.ResumeLayout(false); 
     this.tabG32.ResumeLayout(false); 
     this.tabG33.ResumeLayout(false); 
     this.tabG34.ResumeLayout(false); 
     this.tabG35.ResumeLayout(false); 
     this.tabG36.ResumeLayout(false); 
     this.tabG37.ResumeLayout(false); 
     this.tabG38.ResumeLayout(false); 
     this.tabG39.ResumeLayout(false); 
     this.tabSearch.ResumeLayout(false); 
     this.tabSearch.PerformLayout(); 
     this.grpOptions.ResumeLayout(false); 
     this.grpOptions.PerformLayout(); 
     this.ResumeLayout(false); 
     this.PerformLayout(); 

    } 

    #endregion 

    private System.Windows.Forms.TabControl tabconSubsectors; 
    private System.Windows.Forms.TabPage tabG30; 
    private System.Windows.Forms.TabPage tabG31; 
    private System.Windows.Forms.Label label1; 
    private System.Windows.Forms.TabPage tabG32; 
    private System.Windows.Forms.TabPage tabG33; 
    private System.Windows.Forms.TabPage tabG34; 
    private System.Windows.Forms.TabPage tabG35; 
    private System.Windows.Forms.TabPage tabG36; 
    private System.Windows.Forms.TabPage tabG37; 
    private System.Windows.Forms.TabPage tabG38; 
    private System.Windows.Forms.TabPage tabG39; 
    private System.Windows.Forms.TabPage tabSearch; 
    private System.Windows.Forms.CustomListBox lstG30; 
    private System.Windows.Forms.GroupBox grpOptions; 
    private System.Windows.Forms.RadioButton rbtnHighest; 
    private System.Windows.Forms.RadioButton rbtnCoord; 
    private System.Windows.Forms.CustomListBox lstG31; 
    private System.Windows.Forms.CustomListBox lstG32; 
    private System.Windows.Forms.CustomListBox lstG33; 
    private System.Windows.Forms.CustomListBox lstG34; 
    private System.Windows.Forms.CustomListBox lstG35; 
    private System.Windows.Forms.CustomListBox lstG36; 
    private System.Windows.Forms.CustomListBox lstG37; 
    private System.Windows.Forms.CustomListBox lstG38; 
    private System.Windows.Forms.CustomListBox lstG39; 
    private System.Windows.Forms.Button btnSearch; 
    private System.Windows.Forms.TextBox txtSearch; 
    private System.Windows.Forms.ListBox lstResult; 
} 

}

回答

2

要更改ListBox的排序行为,您必须实现自己的ListBox类。我已经为您编写了解决方案并进行了测试。

右键单击您的项目,选择“添加类...”并输入类名“CustomListBox”。下面的代码添加到类的命名空间之间:

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Windows.Forms; // You need this namespace for ListBox 

public class CustomListBox : ListBox 
{ 
    public CustomListBox() 
     : base() 
    { 
    } 

    protected override void Sort() 
    { 
     if (this.Items.Count > 1) 
     { 
      bool swapped; 
      do 
      { 

       int counter = this.Items.Count - 1; 
       swapped = false; 

       while (counter > 0) 
       { 
        if (this.Items[counter].ToString().CompareTo(
         this.Items[counter - 1].ToString()) == -1) 
        { 
         object temp = Items[counter]; 
         this.Items[counter] = this.Items[counter - 1]; 
         this.Items[counter - 1] = temp; 
         swapped = true; 
        } 

        counter -= 1; 

       } 

      } 
      while (swapped); 
     } 
    } 
} 

所有你需要与你的排序做的是按字母顺序排序。按照上面的示例,您可以使用CompareTo方法来确定在另一个字符串之前或之后出现哪个字符串。

现在你已经有了自己的自定义列表框,您可以将其添加额外的属性和字段:

public class CustomListBox : ListBox 
{ 
    public bool SortByJG; 

    // Other code... 
} 

然后你就可以改变基于当前的排序方式排序的方法来改变搜索如下:

while (counter > 0) 
{ 
    bool swap; 
    if (this.SortByJG) 
    { 
     string[] breakDownCurrent = this.Items[counter].ToString().Split(' '); 
     string[] breakDownPrevious = this.Items[counter - 1].ToString().Split(' '); 
     if (breakDownCurrent[1].CompareTo(breakDownPrevious[1]) == -1) 
     { 
      swap = true; 
     } 
    } 
    else 
    { 
     if (this.Items[counter].ToString().CompareTo(
      this.Items[counter - 1].ToString()) == -1) 
     { 
      swap = true; 
     } 
    } 

    if (swap) 
    { 
     object temp = Items[counter]; 
     this.Items[counter] = this.Items[counter - 1]; 
     this.Items[counter - 1] = temp; 
     swapped = true; 
    } 

    counter -= 1; 

} 

为了使分拣作业中,在ListBox设置Sorted = true;

UPDATE: 由于OP的努力理解,这里的整个类:

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Windows.Forms; // You need this namespace for ListBox 

namespace WindowsApplication1 
{ 

    public class CustomListBox : ListBox 
    { 

     public CustomListBox() 
      : base() 
     { 
     } 

     public bool SortByJG; 

     protected override void Sort() 
     { 

      if (this.Items.Count > 1) 
      { 
       bool swapped; 
       do 
       { 

        int counter = this.Items.Count - 1; 
        swapped = false; 

        while (counter > 0) 
        { 
         bool swap = false; 
         if (this.SortByJG) 
         { 
          string[] breakDownCurrent = this.Items[counter].ToString().Split(' '); 
          string[] breakDownPrevious = this.Items[counter - 1].ToString().Split(' '); 
          if (breakDownCurrent[1].CompareTo(breakDownPrevious[1]) == -1) 
          { 
           swap = true; 
          } 
         } 
         else 
         { 
          if (this.Items[counter].ToString().CompareTo(
           this.Items[counter - 1].ToString()) == -1) 
          { 
           swap = true; 
          } 
         } 

         if (swap) 
         { 
          object temp = Items[counter]; 
          this.Items[counter] = this.Items[counter - 1]; 
          this.Items[counter - 1] = temp; 
          swapped = true; 
         } 

         counter -= 1; 

        } 


       } 
       while (swapped); 
      } 
     } 
    } 
} 

现在也添加自己的方法用指定的方式进行排序:

public void Sort(bool sortByJG) 
{ 
    this.SortByJG = sortByJG; 
    if (this.Sorted) 
    { 
     this.Sort(); 
    } 
    else 
    { 
     this.Sorted = true; 
    } 
} 
+0

right so its lstG30.Sorted = true;得到常规的排序。并按JG排序,您首先会调用该方法。 – Arcadian 2010-06-10 10:46:22

+1

不......如果你实现公共无效排序(布尔sortByJG)方法我建议你可以说list.Sort(true);用于JG排序和list.Sort(false)用于标准排序。这样你可以对两种类型的排序使用相同的方法。 – GenericTypeTea 2010-06-10 10:47:31

+0

和你确定它的JG方法被称为“公共无效排序(布尔sortByJG)”以及常规排序被称为“保护覆盖无效排序()”不会是一个命名冲突,因为它们是都称为“排序” – Arcadian 2010-06-10 10:57:46

5

第一种算法是字母排序一样,这样你就可以直接使用:

int res = string.Compare(first,second); 

Th e第二种算法是降序排列最后两个字符的字母顺序:

int res = -string.Compare(first.Substring(first.Length - 2, 2), second.Substring(first.Length - 2, 2)); 

要排序列表,您有两个选项;首先是创建您自己的ListBox子类并覆盖Sort方法,详见MSDN页面ListBox.Sort Method

第二(更容易和丑陋的)是把一个集合中的所有项目,责令收集和更换的项目列表中,这样的事情:

using System.Collections.Generic; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 { 
    public partial class Form1 : Form { 

     void SortListBox() { 
      List<string> items = new List<string>(); 
      foreach (string value in listBox1.Items) { 
       items.Add(value); 
      } 
      items.Sort((first, second) => string.Compare(first, second)); 
      listBox1.Items.Clear(); 
      listBox1.Items.AddRange(items.ToArray()); 
     } 

    } 
} 

希望这有助于...

+0

你为什么用第一和第二的资源? – Arcadian 2010-06-10 08:16:10

+0

好吧,第一个和第二个是两个刺,比如“G35:45:20:41 JG01”和 “G35:45:58:11 JG07”......你对'你用什么来解决'有什么意思? – 2010-06-10 08:24:04

+0

我认为你只需要算法的帮助,而不是与实际的列表排序......是这种情况? – 2010-06-10 08:24:53