使用嵌套环和子字符串方法(C#)排序的数组

发布于 2025-01-18 01:36:32 字数 799 浏览 3 评论 0原文

我正在尝试使用以数字结尾的字符串输入手动对数组进行排序,我想从最高到最低排序。

例如,我可以从以下输出开始: ,姓名1:1540 ,名称2:2660 ,姓名3:80 ,Name4: 380

最后它应该是这样的: ,名称2:2660 ,姓名1:1540 ,姓名4:380 ,Name3: 80

private string[] OrderHighToLow(string[] data)
    {
        string temp;
        for (int i = 0; i < data.Length; i++)
        {
            for (int y = 0; y < i; y++)
            {
                if (int.Parse(data[y].Substring((data[y].IndexOf(':') + 2))) > int.Parse(data[i].Substring((data[i].IndexOf(':') + 2)))) 
                {
                    temp = data[i];
                    data[i] = data[y];
                    data[y] = temp;
                }
            }
        }
        return data;
    }

这是我测试过的。根据我的说法,这应该可行,但关键是它不行,应用程序只是崩溃了。因此,如果这里有人能弄清楚为什么会这样,我将非常感激。提前致谢。

I am trying to manually sort an array using string inputs that end with a number which I want to sort from highest to lowest.

For example I can start with this output:
,Name1: 1540
,Name2: 2660
,Name3: 80
,Name4: 380

And in the end it should look like this:
,Name2: 2660
,Name1: 1540
,Name4: 380
,Name3: 80

private string[] OrderHighToLow(string[] data)
    {
        string temp;
        for (int i = 0; i < data.Length; i++)
        {
            for (int y = 0; y < i; y++)
            {
                if (int.Parse(data[y].Substring((data[y].IndexOf(':') + 2))) > int.Parse(data[i].Substring((data[i].IndexOf(':') + 2)))) 
                {
                    temp = data[i];
                    data[i] = data[y];
                    data[y] = temp;
                }
            }
        }
        return data;
    }

This is what I have tested. According to me, this should work, but the point is it doesn't, the application just crashes. So, if anyone here can figure out why that may be, I would be very thankful. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在夏天 2025-01-25 01:36:32

您的比较结果小于

int.Parse(data[y].Substring((data[y].IndexOf(':') + 2)))

不大于

 if (int.Parse(data[y].Substring((data[y].IndexOf(':') + 2))) < int.Parse(data[i].Substring((data[i].IndexOf(':') + 2)))) 

your comparison is worng

int.Parse(data[y].Substring((data[y].IndexOf(':') + 2)))

less than Not greater than

 if (int.Parse(data[y].Substring((data[y].IndexOf(':') + 2))) < int.Parse(data[i].Substring((data[i].IndexOf(':') + 2)))) 
静谧 2025-01-25 01:36:32

这应该在system.linq的帮助下完成工作:

private string[] OrderHighToLow(string[] data)
{
    //create a temporary dictionary, which makes sorting easier
    var tmpMap = new Dictionary<string, int>();
    foreach (var item in data)
    {
        //split the string 
        var elems = item.Split(":");
        //add string and int pair to the dictionary
        tmpMap.Add(elems[0], int.Parse(elems[1]));
    }

    //sort Dictionary by value and use select to rebuild the string, then convert it back to an array
    return tmpMap.OrderByDescending(x => x.Value).Select(x => $"{x.Key}: {x.Value}").ToArray();
}

希望这会有所帮助。

This should do the job with the Help of System.Linq:

private string[] OrderHighToLow(string[] data)
{
    //create a temporary dictionary, which makes sorting easier
    var tmpMap = new Dictionary<string, int>();
    foreach (var item in data)
    {
        //split the string 
        var elems = item.Split(":");
        //add string and int pair to the dictionary
        tmpMap.Add(elems[0], int.Parse(elems[1]));
    }

    //sort Dictionary by value and use select to rebuild the string, then convert it back to an array
    return tmpMap.OrderByDescending(x => x.Value).Select(x => 
quot;{x.Key}: {x.Value}").ToArray();
}

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文