排序数组的问题 [英] Problem with sorting array

查看:54
本文介绍了排序数组的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是这个论坛的新手,也刚刚开始使用c#,我正在尝试编写一些程序,您可以动态分配存储在数组中的按钮,每次点击一个按钮,它会显示一个按钮的编号一个标签。我还需要更新点击按钮的频率,最后根据一个按钮对它们进行排序,从最小到最大。所以我不能使用排序方法我只能使用嵌套for循环。

这是我的代码到目前为止,每次点击按钮进行排序它只给我一个数字?

谢谢



lstNum是一些分配的按钮,freqArray是一个按钮数组,而lbl.Out.Digits是标签,其中出现点击按钮的数量在那里我需要显示点击按钮的排序列表。



int temp = 0;

for(int i = 0; i< ; lastNum; i ++)

{

for(int j = i + 1; j< lastNum; j ++)

{

if(freqArray [i]> freqArray [j])

{

temp = freqArray [i];

freqArray [i] = freqArray [j];

freqArray [j] = temp;

}



}



lblOutDigits.Text = freqArray [i] .ToString();

}

解决方案

好的,有更简单的方法可以做到这一点。

首先要做的是尝试使用Button保持点击计数 - 这样可以更容易处理。幸运的是,每个Control(和一个Button是一个控件)都有一个备用信息,您可以将其用于任何目的:标记属性。

因此,首先在加载表单时将所有按钮的标记设置为零,然后在Click事件处理程序中更新它。同样,框架在这里有所帮助,因为它为我们提供了被点击作为事件处理程序参数之一的按钮:

  private   void  myButton_Click( object  sender,EventArgs e)
{
按钮userClickedThis =发件人 as 按钮;
if (userClickedThis!= null
{
int clickedThisManyTimes =( int )userClickedThis.Tag;
userClickedThis.Tag = clickedThisManyTimes + 1 ;
...
}
}



您现在需要做的就是将按钮排序为标记顺序,然后您就可以构建您的订单字符串。

 字符串 orderedList =  ; 
foreach (按钮b in freqButtons)
{
orderedList + = b.Text + ;
}
lblOutDigits.Text = orderedList;


您是否考虑过使用Linq lambda表达式研究一种方法?你不需要硬输入所有代码。







 使用 System.Linq; 





如果你想要一个合适的解决方案,你应该为点击添加属性。如果这些按钮不是动态创建的,请考虑创建一个名为ButtonClass的类,它具有相应的属性:



  public   class  ButtonClass 
{
public string BtnName { get ; set ; }
public int TimesClicked { get ; set ; }
}





你可以使用上面提供的Linq正确排序:

列表< buttonclass> list =  new  List< buttonclass>(); 
list.OrderByDescending(o = > o.TimesClicked);
< / buttonclass > < / buttonclass >


Hi I am new on this forum and also just started with c#, I am trying to write some program where you have dynamically allocated buttons stored in an array and every time u click on a button it show you number of a button in a label. also I needed to update the frequency of clicked buttons and at the end to sort them from smallest to largest buy clicking on one button. So I cant use sort method I only can use nested for loops.
This is my code so far and every time I click on button for sorting it gives me only one number??
Thank you

lstNum is a number of allocated buttons and freqArray is an array of buttons, and lbl.Out.Digits is label where number of clicked button appear and there I need to display sorted list of clicked buttons.

int temp = 0;
for (int i = 0; i < lastNum; i++)
{
for (int j = i + 1; j < lastNum; j++)
{
if (freqArray[i] > freqArray[j])
{
temp = freqArray[i];
freqArray[i] = freqArray[j];
freqArray[j] = temp;
}

}

lblOutDigits.Text = freqArray[i].ToString();
}

解决方案

OK, there are easier ways to do this.
The first thing to do is to try keeping your "clicked count" with the Button - that makes it easier to handle. Fortunately, every Control (and a Button is a Control) has a "spare" bit of information which you can use for any purpose you want: the Tag property.
So, start by setting the Tag of all your buttons to zero when you load your form, and then update it in the Click Event handler. Again, the framework helps here, because it provides us with teh Button that was clicked as one of the Event handler parameters:

private void myButton_Click(object sender, EventArgs e)
    {
    Button userClickedThis = sender as Button;
    if (userClickedThis != null)
        {
        int clickedThisManyTimes = (int)userClickedThis.Tag;
        userClickedThis.Tag = clickedThisManyTimes + 1;
        ...
        }
    }


All you need to do now is sort your Buttons into Tag order, and you can build your order string.

string orderedList = "";
foreach (Button b in freqButtons)
    {
    orderedList += b.Text + " ";
    }
lblOutDigits.Text = orderedList;


Have you considered studying a way to do this using Linq lambda expressions? You won't need to hard type all the code.



using System.Linq;



If you want a proper solution, you should add attributes to that "click". If those buttons are not dynamically created, please consider creating a class named ButtonClass or something, that has the appropriate properties:

public class ButtonClass
    {
        public string BtnName { get; set; }
        public int TimesClicked { get; set; }
    }



that way you can properly sort using the Linq I provided above:

List<buttonclass> list = new List<buttonclass>();
list.OrderByDescending(o => o.TimesClicked);
</buttonclass></buttonclass>


这篇关于排序数组的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆