Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
119 views
in Technique[技术] by (71.8m points)

c# - Custom listbox sorting

I need to sort the data contained within a number of listboxes. The user will be able to select between two different types of sorting using radio boxes, one of which is checked by default on form load. I have created the IF statements needed in order to test whether the checked condition is true for that radio button. but i need some help to create the custom sort algorithms.

Each list with contain similar looking data, the only difference in the prefix with which each line starts. For example each line in the first listbox starts with the prefix "G30" and the second listbox will be "G31" and so on. There are 10 listboxes in total (G30-G39 in terms of prefixes).

The first search algorithm has to sort the lines by the number order of the first 13 chars.

Example: This is how the data looks before sorting

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

and this is how it should look after sorting

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

as you can see, the prefixes are the same. so it is sorted, lowest first, by the next pair integers, then the next pair and the next but not by the value after "JG".

the second sort algorithm will ignore the first 13 chars and sort by order of the value after "JG", highest first.

any help? theres some rep in it for you :)

thanks in advance

Edit

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";
        t

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The first algorithm is the same as alphabetic ordering, so you can use directly:

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

The second algorithm is descending alphabetic ordering of the last two chars:

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

To sort the list you have two options; the first is to create your own ListBox subclass and override the Sort method as detailed on the MSDN page for ListBox.Sort Method.

The second (easier and uglier) is to put all the items in a collection, order the collection and replace the items in the list, something like this:

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());
        }

    }
}

Hope this helps...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...