如何在C#中使用排序 [英] How Do I Use Sorting In C#

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

问题描述

我正在使用c#语言并在基于控制台的应用程序上执行此操作。我遇到的问题是排序。我需要生产两件东西。首先是按字母顺序排序,第二是按数字排序。我自己添加产品和数量,因此没有固定的arraylist。我想知道如何根据我进入控制台的内容对其进行排序。这有意义吗?

  public   class  TheoVendingMachine 
{
私人 ArrayList商品;
public MoneySet资金;
public MoneySet currentMoneys;
public TheoVendingMachine()
{
this .merchandises = < span class =code-keyword> new ArrayList();
this .moneys = new MoneySet();
this .currentMoneys = new MoneySet();
}
public Merchandise [] getProductTypes()
{

ArrayList arrayList = new ArrayList();
arrayList.Sort();
foreach (商品 .merchandises)
{
if (!arrayList.Contains(product))
{
arrayList.Add (产品);
}

}

商品[]数组= 商品[arrayList.Count];

for int i = 0 ; i < arrayList.Count; i ++)
{
array [i] =(Merchandise)arrayList [i ]。
arrayList.Sort();
}


return 数组;
}

解决方案

您在控制台中输入的内容始终是文本 - 所以如果您想要对事物进行排序在数字上,您需要将文本转换为数字:

  string  s = Console.ReadLine() ; 
int i;
if int .TryParse(s, out i))
{
// 输入的有效整数
...
}
else
{
// 告诉用户不是数字......
...
}





我不会使用ArrayList - 使用类型化数组或List< T> - 这样你就不必在使用它之前把它投射到所需的类型。

所以...设置一个类来包含字符串和整数值,然后使用List< T>该类,或者设置两个列表(一个字符串和一个int)并对它们进行适当的排序。



你知道怎么做,是吗?


I am using c# language and doing this on console based application. The problem i am having is sorting. i need to produce two things. first is sorting alphabetically, second is sorting numerically. I add products and quantity myself so there is no fixed arraylist as such. i want to know how to sort it based on what i enter into the console. does this make sense?

     public class TheoVendingMachine
  {
      private ArrayList merchandises;
      public MoneySet moneys;
      public MoneySet currentMoneys;
      public TheoVendingMachine()
      {
          this.merchandises = new ArrayList();
          this.moneys = new MoneySet();
          this.currentMoneys = new MoneySet();
      }
      public Merchandise[] getProductTypes()
      {

          ArrayList arrayList = new ArrayList();
          arrayList.Sort();
          foreach (Merchandise product in this.merchandises)
          {
              if (!arrayList.Contains(product))
              {
                  arrayList.Add(product);
              }

          }

Merchandise[] array = new Merchandise[arrayList.Count];

          for (int i = 0; i < arrayList.Count; i++)
          {
              array[i] = (Merchandise)arrayList[i];
              arrayList.Sort();
          }


          return array;
      }

解决方案

What you enter in the console is always text - so if you want to sort things numerically, you need to convert the text to a number:

string s = Console.ReadLine();
int i;
if (int.TryParse(s, out i))
   {
   // Valid integer entered
   ...
   }
else
   {
   // Tell the user that isn't a number...
   ...
   }



And I wouldn't use an ArrayList - use a typed array or List<T> - that way you don;t have to cast it to the desired type before you use it.
So...Set up either a class to contain the string and integer value together then use a List<T>of that class, or set up two Lists (one of string and one of int) and sort them appropriately.

You know how to do that, yes?


这篇关于如何在C#中使用排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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