C#:链接字符串[]和int []元素呆在一起排序时,INT [] [英] C#: Linking string[] and int[] elements to stay together when sorting int[]

查看:94
本文介绍了C#:链接字符串[]和int []元素呆在一起排序时,INT []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我需要人的姓名和年龄,它们存储在两个独立的阵列,按年龄排序,然后显示在列表框的结果。我所拥有的一切,直到把它放回列表框。这听起来很简单,但foreach循环(从我的C#的非常基本的理解)只能走阵列之一的值。我需要的姓名和年龄在同一行,因为他们连在一起,所以我决定切换到一个for循环。不过,即使我对时代的排序工作得很好,我不能让名年龄相匹配。下面是我的一些code的:

So I need to take names and ages of people, store them in two separate arrays, sort them by age, and then display the results in a listbox. I have everything down until putting it back into the listbox. It sounds simple, but the foreach loop (from my very basic understanding of C#) can only take the values of one of the arrays. I need the names and ages on the same line because they're linked together, so I decided to switch to a for loop. However, even though my sorting on the Ages works just fine, I can't make the name match the age. Here is some of my code:

    const int MAX = 100;
    int count = 0;

    int[] Age = new int[MAX];
    string[] NameEntry = new string[MAX];

只是为了显示你我是如何声明的数组。这里是我的商店click事件:

Just to show you how I declared the arrays. Here is my "store" click event:

    int age;

        if (txtName.Text == "")
        {
            lblWarningMessage.Text = "There is an error with your entries. A name must be entered!";
            btnClearWarn.Show();
        }
        else
        {
            NameEntry[count] = txtName.Text;

            if (int.TryParse(txtAge.Text, out age))
            {
                Age[count] = age;

                txtAge.Clear();
                txtName.Clear();
                txtName.Focus();

                lstbxResults.Items.Add(NameEntry[count] + " is " + Age[count].ToString() + " years old.");

                count++;
            }
            else
            {
                lblWarningMessage.Text = "There is an error with your entries. The Age entry must be an integer.";
                btnClearWarn.Show();
            }
        }

最后,排序操作和随后的for循环,将其添加到我的列表框

And finally, the sorting operation and subsequent for loop to add it to my listbox:

        Array.Resize(ref NameEntry, count);
        Array.Resize(ref Age, count);

        lstbxResults.Items.Clear();
        int minAge = 0;
        int minIndex = 0;

        for (int a = 0; a < Age.Length - 1; a++)
        {
            minAge = Age[a];
            minIndex = a;

            for (int b = a + 1; b < Age.Length; b++)
            {
                if (Age[b] < minAge)
                {
                    minAge = Age[b];
                    minIndex = b;
                }
            }

            OrderByAges(ref Age[minIndex], ref Age[a]);
        }

        for (int c = 0; c < Age.Length; c++)
        {
            lstbxResults.Items.Add(NameEntry[c] + " is " + Age[c] + " years old.");
        }
    }

    private void OrderByAges(ref int p1, ref int p2)
    {
        int temp = p2;
        p2 = p1;
        p1 = temp;
    }

是的,我认识的Array.sort会更快,但是这个有异曲同工之妙,这是我奉命去做。如何将元素NameEntry到时代的链接,并拥有它时,它的排序与时代一起变化?有什么想法

Yes, I realize Array.sort would be faster, but this serves the same purpose, and this is how I was instructed to do it. Any thoughts on how to link the element in "NameEntry" to "Age", and have it change along with the Age when it is sorted?

推荐答案

您真的应该在这种情况下使用的Array.Sort。它能够共同排序在两个相连的数组键和值,如:

You really should use "Array.Sort" in this case. It is capable of co-sorting keys and values in two linked arrays, as in:

Array.Sort(Age, NameEntry, 0, count);

如果你的老师真的坚持不使用的Array.Sort一个解决方案,只需使用一个快速排序的实现,简单地交换密钥和值的元素在同一时间:

If your teacher really insists on a solution that does not use Array.Sort, just use a quicksort implementation that simply swaps the elements of both key and value at the same time:

这样的(未测试):

public static class CoSorter
{
    public static void Sort<TKey, TValue>(this TKey[] keys, TValue[] values, int start, int count)
    {
        QuickCosort(keys, values, start, count - 1, Comparer<TKey>.Default);
    }

    public static void Sort<TKey, TValue>(this TKey[] keys, TValue[] values, int start, int count, IComparer<TKey> comparer)
    {
        QuickCosort(keys, values, start, count - 1, comparer);
    }

    private static void QuickCosort<TKey, TValue>(TKey[] keys, TValue[] values, int left, int right, IComparer<TKey> comparer)
    {
        int i = left, j = right;
        var pivot = keys[(left + right) / 2];

        while (i <= j)
        {
            while (comparer.Compare(keys[i], pivot) < 0)
            {
                i++;
            }

            while (comparer.Compare(keys[j], pivot) > 0)
            {
                j--;
            }

            if (i <= j)
            {
                // Swap
                var tmpKey = keys[i];
                var tmpVal = values[i];
                keys[i] = keys[j];
                values[i] = values[j];
                keys[j] = tmpKey;
                values[j] = tmpVal;

                i++;
                j--;
            }
        }

        // Recursive calls
        if (left < j)
        {
            QuickCosort(keys, values, left, j, comparer);
        }

        if (i < right)
        {
            QuickCosort(keys, values, i, right, comparer);
        }
    }
}

另外,你可能要考虑这个有点难,看看也许你需要的数据结构,而不是属于一个整体值的两个数组。

Alternatively you may want to think about this a bit harder and see if maybe you need a data structure instead of two arrays of values that belong together.

这篇关于C#:链接字符串[]和int []元素呆在一起排序时,INT []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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