添加新值后对数组进行排序

时间:2017-09-01 16:04:21

标签: c# .net

所以我必须编写这个算法,在数组中添加一个值,然后再次对其进行排序

我必须使用一个名为“compareTo”的方法,它返回以下值:-1,0和1

如果它返回-1,则意味着我们尝试添加的对象在比较

时来自另一个对象

如果它返回0,则项目在同一地点(我不明白)

如果返回1则表示该项目的位置在数组

中的该对象后面

我将如何重新调整阵列我真的很难受哈哈,有人能给我一个正确的方向吗?

这就是我的排序功能现在的样子

`  public void sortArray(NAW item)
    {
        for (int i = 0; i < _size; i++)
        {
            //comparing every object in array



            if (_nawArray[i].CompareTo(item) == -1)
            {
                //it needs to be in front of the object

            }

            if (_nawArray[i].CompareTo(item) == 0)
            {
                //it needs to be on the same spot as the object

            }

            if (_nawArray[i].CompareTo(item) == 1)
            {
                //it needs to be behind the object

            }
        }
    }


`

比较功能

   public int CompareTo(NAW andereNaw)
        {
            // signal compare to Logger
            Logger.Instance.LogCompare();

            if (andereNaw.Naam == Naam && andereNaw.Adres == Adres && andereNaw.Woonplaats == Woonplaats)
            {
                return 0;
            }
            else
            {
                if (andereNaw.Woonplaats != Woonplaats)
                { // woonplaatsen zijn verschillend
                    return Woonplaats.CompareTo(andereNaw.Woonplaats);
                }
                else if (andereNaw.Naam != Naam)
                { // woonplaatsen zijn verschillend en namen zijn verschillend
                    return Naam.CompareTo(andereNaw.Naam);
                }
                else
                { // woonplaatsen en namen zijn gelijk
                    return Adres.CompareTo(andereNaw.Adres);
                }
            }

        }

数组中的项目

        NawArrayUnordered array = new NawArrayUnordered(20);
        array.Add(new NAW("Persoon 1", "Adres 1", "Woonplaats 1"));
        array.Add(new NAW("Persoon 2", "Adres 2", "Woonplaats 2"));
        array.Add(new NAW("Persona non grata", "Adres 3", "Woonplaats 3"));
        array.Add(new NAW("Persoon 4", "Adres 4", "Woonplaats 2"));
        array.Add(new NAW("Persoon 1", "Adres 5", "Woonplaats 1"));
        array.Add(new NAW("Persoon 2", "Adres 6", "Woonplaats 2"));
        array.Add(new NAW("Persona non grata", "Adres 7", "Woonplaats 3"));
        array.Add(new NAW("Persoon 2", "Adres 8", "Woonplaats 2"));
        array.Add(new NAW("Persoon 9", "Adres 9", "Woonplaats 1"));
        array.Add(new NAW("Persoon 10", "Adres 10", "Woonplaats 2"));

1 个答案:

答案 0 :(得分:1)

我想你可以使用这段代码:

public static T[] AddItemToSortedArray<T>(T[] array, T item) where T : IComparable<T>
{
    var result = new T[array.Length + 1];
    var index = 0;
    while (index < array.Length && array[index].CompareTo(item) == -1)
    {
        result[index] = array[index];
        ++index;
    }

    result[index++] = item;

    while (index < result.Length)
    {
        result[index] = array[index - 1];
        ++index;
    }

    return result;
}

为简单起见,我使用int作为T
用法:

var array = new[] {6, 9, 11, 14, 19, 22, 23, 28, 45, 47};
var item = 31;
var result = AddItemToSortedArray(array, item);
// result would be {6, 9, 11, 14, 19, 22, 23, 28, 31, 45, 47}