如何在.net中将(三个,两个,一个)排序为(一,二,三)? [英] How to sort (three, two, one) as (one, two, three) in .net?

查看:63
本文介绍了如何在.net中将(三个,两个,一个)排序为(一,二,三)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,我有一个任务,按照字母格式对数字进行排序例如:



Hi Friends, I have one task, sort the number, that are in alphabetic format For example:

(one,three,four,seven,eight)







此内容排序如下这个






this content sorted like this

(eight, four, three, seven, one)





任何可能的方式,以正确的格式对此进行排序,如下所示





any possible way available for sort this in correct format like below

one, three, four, seven, eight

推荐答案

没有内置功能可以简短数字由他们的名字,但只有价值...你得到的结果当然是字母排序...

唯一的方法是编写自己的排序方法,知道如何处理这种情况...

可能的一种方法是使用List< T>的Sort(IComparer)方法来处理问题...

https://msdn.micros oft.com/en-us/library/234b841s(v=vs.110).aspx [ ^ ]
There is no built in functionality to short numbers by their names, but only by values...The result you got is of alphabetic sorting of course...
The only way is to write your own sort method that knows how to handle this case...
A possible ease may be using List<T>'s Sort(IComparer) method to handle the issue...
https://msdn.microsoft.com/en-us/library/234b841s(v=vs.110).aspx[^]


当你有一个字符串数组时,排序将使用的比较将基于字符串内容的逐字符检查 - 因此您需要将字符串值转换为其等效的数值,然后才能对它们进行排序。不幸的是,没有内置的方法可以做到这一点。



一个解决方案是创建一个数字名称的集合并使用Linq:

When you have an array of strings, the comparison that sorting would use would be based on a character-by-character examination of the string content - so you need to convert the string values to their equivalent numeric values before you can order them. Unfortunately, there is no built in way to do that.

One solution would be to create a collection of the number names and use Linq:
            private List<string> names = new List<string>() { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" };
...
            string[] unsorted = new string[] { "eight", "four", "three", "seven", "one" };
            string[] sorted = unsorted.OrderBy(s => names.IndexOf(s)).ToArray();

要按降序获取值,您要么颠倒初始数组中的顺序,要么使用Linq Reverse方法。



但是......对于大量的数字来说,这将是很多工作!



错别字[/ edit ]

To get the values in descending order, you'd either reverse the order in the initial array, or use the Linq Reverse method.

But...This would be a lot of work to use for a significant range of numbers!

[edit]Typos[/edit]


除了 Kornfeld Eliyahu Peter <的解决方案1之外/ a> [ ^ ] i建议使用词典 [ ^ ]对象与Linq一起。



In addition to solution 1 by Kornfeld Eliyahu Peter[^] i do recommend to use Dictionary[^] object tohgether with Linq.

//create dictionary object
//Key - integer value, Value - string representatioin of number
Dictionary<int, string> dic = new Dictionary<int, string>();
//add values
dic.Add(1, "one");
dic.Add(2, "two");
dic.Add(3, "three");
dic.Add(4, "four");
dic.Add(5, "five");
dic.Add(6, "six");
dic.Add(7, "seven");
dic.Add(8, "eight");
dic.Add(9, "nein");
dic.Add(10, "ten");

//your list
List<string> mynumbers = new List<string>(){"eight", "four", "three", "seven", "one"};

//Linq query to sort data by numeric values
var resultlist = from mn in mynumbers
    join d in dic on mn equals d.Value
    orderby d.Key
    select mn;

//result list is IEnumerable collection of string:
//one 
//three 
//four 
//seven 
//eight 


这篇关于如何在.net中将(三个,两个,一个)排序为(一,二,三)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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