c# - 为什么我不能选择排序这个库?

标签 c# arrays sorting selection

我正在尝试对图书馆的图书使用选择排序,以便按字母顺序对它们进行排序,但我无法让它工作。

选择排序(图书馆);不起作用,但 SelectionSort(titles);有什么想法吗?谢谢:)

完整代码如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace BookSortingEx
{
    class Program
    {

        static void swap<T>(ref T x, ref T y)
        {
            //swapcount++;
            T temp = x;
            x = y;
            y = temp;
        }


        static void printArray(string[] a)
        {
            for (int i = 0; i < a.Length; i++)
            {
                Console.Write(a[i] + ",");
            }
            Console.WriteLine();
        }

        static bool IsInOrder<T>(T[] a) where T : IComparable
        {
            for (int i = 0; i < a.Length - 1; i++)
            {
                if (a[i].CompareTo(a[i + 1]) > 0)
                    return false;
            }
            return true;
        }

        static void Main(string[] args)
        {

            string[] array1 = { "Fred", "Zoe", "Angela", "Umbrella", "Ben" };
            string[] titles = {"Writing Solid Code",
                "Objects First","Programming Gems",
                "Head First Java","The C Programming Language",
                "Mythical Man Month","The Art of Programming",
                "Coding Complete","Design Patterns", 
                "Problem Solving in Java"};
            string[] authors = { "Maguire", "Kolling", "Bentley", "Sierra", "Richie", "Brooks", "Knuth", "McConnal", "Gamma", "Weiss" };
            string[] isbns = { "948343", "849328493", "38948932", "394834342", "983492389", "84928334", "4839455", "21331322", "348923948", "43893284", "9483294", "9823943" };

            Book[] library = new Book[10];

            for (int i = 0; i < library.Length; i++)
            {
                library[i] = new Book(isbns[i], titles[i], authors[i]);
            }

           **DOESNT WORK - // SelectionSort(library);** 

           SelectionSort(titles);
            printArray(titles);

            foreach (Book book in library)
            {
                Console.WriteLine(" {0} ", book);
            }
            Console.WriteLine();
            Console.ReadKey();

        }

        static public void SelectionSort(string[] a)
        {
            for (int i = 0; i < a.Length - 1; i++)
            {
                int smallest = i;
                for (int j = i + 1; j < a.Length; j++)
                {
                    if (a[j].CompareTo(a[smallest]) < 0)
                        smallest = j;
                }
                swap(ref a[i], ref a[smallest]);
            }
        }
    }
}

最佳答案

您的方法需要一个 string[],但您给它一个 Book[]

您需要更改 SelectionSort 的实现以支持 Comparable Collections,并让 Book 实现 IComparable 接口(interface)。

public class Book : IComparable
{
    // Implementation

    public int CompareTo(Book otherBook)
    {
        // Implementation
    }
}

static public void SelectionSort<T>(IList<T> a) where T : IComparable
{
    // Implementation
}

这种方法的优点是,您不必为要排序的每种对象集合创建不同版本的 SelectionSort

关于c# - 为什么我不能选择排序这个库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15389721/

相关文章:

c# - 屏幕抓取

c# - 使用 Json.NET 解析 Json 字符串

ios - 可以在 objective c 中使用 swift 中的 array reduce 概念吗?

java - 转置矩阵存储在一维数组中而不使用额外的内存

sql - 大型数据集的排序算法

c# - 在 C# 项目中管理数据库更改

c# - 从 TCP Socket 接收和发送连续数据

ruby-on-rails - Rails 4 Postgres 数组的验证

ios - 在 swift 中为多维数组添加数组扩展?

c++ - 排序并跟踪元素