Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C列表排序问题_C#_Sorting - Fatal编程技术网

C# C列表排序问题

C# C列表排序问题,c#,sorting,C#,Sorting,我有一个列表,如下所示: List<string> newList = new List<string>() { "10S", "XS", "80", "5S", "160", "40S", "80S", "STD", "40", "XXS" }; 我想把它分类 {40、80、160、5S、10S、40S、80S、STD、XS、XXS}; 我该怎么做?希望任何人都能在这个问题上帮助我,谢谢 我写了一些代码,它能正常工作。我只是用Linq做你想做的事 using Syst

我有一个列表,如下所示:

List<string> newList = new List<string>() { "10S", "XS", "80", "5S", "160", "40S", "80S", "STD", "40", "XXS" };
我想把它分类

{40、80、160、5S、10S、40S、80S、STD、XS、XXS};
我该怎么做?希望任何人都能在这个问题上帮助我,谢谢

我写了一些代码,它能正常工作。我只是用Linq做你想做的事

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace SortTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //your objects
            List<string> newList = new List<string>() { "10S", "XS", "80", "5S", "160", "40S", "80S", "STD", "40", "XXS" };

            //filter the stuff you want first, and then sort them from small to big
            var sQuery = newList.Where(p => p.EndsWith("s", StringComparison.CurrentCultureIgnoreCase)).OrderBy(p => p);
            var numQuery = newList.Where(p => Regex.IsMatch(p, "^[0-9]+$", RegexOptions.Singleline)).OrderBy(p => p);
            var otherQuery = newList.AsQueryable().Where(p => !sQuery.Contains(p) && !numQuery.Contains(p));

            //get the result, add the sorts
            List<string> resultList = new List<string>();
            resultList.AddRange(numQuery);
            resultList.AddRange(sQuery);
            resultList.AddRange(otherQuery);

            //print them out
            Console.Write(string.Join(",", resultList.ToArray()));

            Console.WriteLine();
            Console.ReadKey();
        }
    }
}

新列表现在是:{40,80,160,5S,10S,40S,80S,STD,XS,XXS}

您的副本将必须覆盖排序功能并编写自己的。我认为默认排序不会按照您想要的方式进行排序。您需要定义一个比较函数,该函数返回一个值,该值指示您希望排序的顺序。所有预定义的比较都不适用于此。是否有我看不到的特定排序规则?所以你想要一个asc数字排序,然后是asc字母+数字,然后是asc字母排序?看看这里=>正如大家提到的,你需要创建你自己的比较函数,这个链接应该给你一个想法。很好的解决方案。您甚至可以在过滤掉这些数字后立即进行排序,比如:var newList=from list-in-list-in-list-where-int.TryParseitem,out-temp-select-item.OrderByx=>int.Parsex.ToList;但也许你在这里看到的是教学价值,而不是简洁。
List<string> list = new List<string>() { "10S", "XS", "80", "5S", "160", "40S", "80S", "STD", "40", "XXS" };

// filter out numbers:
int temp;
var newList = (from item in list where int.TryParse(item, out temp) select item).ToList();

// sort by number and get back string:
newList = newList.Select(x => int.Parse(x)).OrderBy(x => x).Select(x => x.ToString()).ToList();

// sort the rest by string:
var second = list.Except(newList).OrderBy(x => x).ToList();

// Merge the two back together
newList.AddRange(second);