使用C#排序字符串列表 [英] Sorting A List of Strings Using C#

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

问题描述

你好专家,



这对你来说可能很简单,但这让我很难过。我正在尝试排序字符串列表。我找到了varios示例如何对字符串数组进行排序,但是我怎么不对字符串列表进行排序。请帮助我,因为这可能只需要5分钟,但它已经花了我2天!我遇到的问题是:它正在检查每个循环但从未进入。我认为这可能与Icomparable有关我从一些文章中注意到了没有警告没有错误:/ ??



感谢您的帮助。请找到我正在使用的代码:

Hello Experts,

This may be very simple for you, but this is giving me a very hard time. I am trying to Sort An LIST of strings. i found varios examples how to sort an array of strings but none of how can i sort an list of strings. please help me as this may only take 5min for you but it already took me 2 days!The problem i am having is that: it is checking each while loop but never getting in. I think this might have something to do with the Icomparable as i noticed from some articles. No warning no errors :/ ??

Thanks for your help. Please find the code i am using:

**********************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace myDemo
{
    class Program
    {
        static void Main()
        {
            string[] unsorted = { "z", "e", "x", "c", "m", "q", "a" };

            List<string> myList = new List<string>();
            myList.AddRange(unsorted);

            for (int i = 0; i < myList.Count; i++)
            {
                Console.WriteLine(myList[i]);
            }

            Console.WriteLine("Sorted");

            List<string> Sorted = Sorting(myList, 0, myList.Count - 1);

            for (int i = 0; i < Sorted.Count; i++)
            {
                Console.WriteLine(Sorted[i]);
            }
            Console.ReadKey();
        }

        public static List<string> Sorting(List<string> Unsorted, int lowerLimit, int upperLimit)
        {
            if (Unsorted.Count <= 2)
            {
                return Unsorted;
            }

            //Creating Needed Variables
            int Pivot = Unsorted.Count / 2; //with return an integer not a decimal number, if input is integer
            int Lower = lowerLimit, Greater = upperLimit;

            while (Lower <= Greater)
            {
                while (Unsorted[Lower].CompareTo(Unsorted[Pivot]) < 0)
                {
                    Lower++;
                }
                while (Unsorted[Greater].CompareTo(Unsorted[Pivot]) > 0)
                {
                    Greater--;
                }

                if (Lower == Greater)
                {
                    //swap the elements
                    string temp = Unsorted[Lower];
                    Unsorted[Lower] = Unsorted[Greater];
                    Unsorted[Greater] = temp;

                    Lower++;
                    Greater--;
                }
            }

            if (lowerLimit < Greater)
            {
                Sorting(Unsorted, lowerLimit, Greater);
            }
            if (Lower < upperLimit)
            {
                Sorting(Unsorted, Lower, upperLimit);
            }

            return Unsorted;
        }
    }
}

推荐答案

算法从一开始就是错误的,签名,因为排序方法不应该取决于任何初始索引。输入应该得到一个列表参数,没有其他内容。在某些实现中,您将需要为递归调用创建另一个函数。



我不知道你使用了什么算法描述,但是算法的不同版本有伪代码,非常简单:

http://en.wikipedia.org/wiki/Quick_sort [ ^ ]。



另外,你呢?在开发过程中使用调试器?你应该。问题很简单,你应该最好解决它。自己。







我拿了原始代码(仍然,它是'不够好,你应该做我提到的有关签名的内容),在我将其更改为使用 List 后立即解决。首先,使用 System.Collections.Generic.List< IComparable> ,与原始代码一样。将数组声明更改为列表,使用 AddRange 添加列表的填充,并且不做任何其他操作。最重要的是,不返回未排序的。该算法适用于数组,因此它不使用任何列表功能,也不创建任何数组,因此您不应创建任何列表。该算法就地工作。 使返回类型无效。在排序后显示相同的列表,如下所示 - 它将被排序。



-SA
The algorithm is wrong from the very beginning, the signature, as the sort method should not depends on any initial indices. The input should get a list parameter and nothing else. In some implementations, you will need to create another function for recursive calls.

I don''t know what algorithm description did you use, but there are the pseudo-codes for different versions of the algorithm, pretty simple:
http://en.wikipedia.org/wiki/Quick_sort[^].

Also, do you use the debugger during development? You should. The problem is pretty simple, you should better solve it to the very end. By yourself.



I took the original code (still, it''s not good enough, you should do what I mentioned about the signature), it worked out immediately after I changed it to use List. First, use System.Collections.Generic.List<IComparable>, as in original code. Change array declaration into list, add population of the list using AddRange and don''t do anything else. Most importantly, do not return unsorted. The algorithm works on arrays, so it does not use any list features and does not create any arrays, so you should not create any lists. The algorithm works in-place. Make the return type void. Show the same list after sorting as the one you''ve shown before — it will be sorted.

—SA


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

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