如何在C#中使用排序

编程入门 行业动态 更新时间:2024-10-25 00:32:09
本文介绍了如何在C#中使用排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

public class TheoVendingMachine { 私人 ArrayList商品; public MoneySet资金; 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 (商品 此 .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#中使用排序

本文发布于:2023-11-03 06:39:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554469.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!