简体   繁体   English

如何在C#中按升序对这些对象数组进行排序?

[英]How do I sort these arrays of objects in ascending order in C#?

Here is my array of objects 这是我的对象数组

Game[] gameConsoles = new Games[5];
// paramaters are name of the console and game ID
gameConsoles[0] = new Games("Playstation 4", 101);
gameConsoles[1] = new Games("Xbox 1", 108);
gameConsoles[2] = new Games("PS Vita", 110);
gameConsoles[3] = new Games("Wii U", 104);
gameConsoles[4] = new Games("3DS", 102);

for (int i = 0; i < gameConsoles.Length; i++)
{
    gameConsoles[i].display();
}

It will basically display all 5 objects in each message box but how do I make it so that it can display them based on the game ID order in ascending? 它基本上会在每个消息框中显示所有5个对象,但是我如何制作它以便它可以根据游戏ID顺序以升序显示它们?

My sorting algorithm I used when I sorted a regular array of numbers. 我在排序常规数字时使用的排序算法。

public void ascendingOrder()
{
    // helper class
    double temp = 0;

    for (int j = 0; j < numbers.Length; j++)
    {
        for (int i = 0; i < numbers.Length - 1; i++)
        {
            if (numbers[i] > numbers[i + 1])
            {
                temp = numbers[i];
                numbers[i] = numbers[i + 1];
                numbers[i + 1] = temp;
            }
        }
    }
}

You can use use LINQ OrderBy . 您可以使用LINQ OrderBy

foreach(var item in gameConsole.OrderBy(r=> r.GameID))
{
   item.display();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            SortedDictionary<int, Game> gameConsoles = new SortedDictionary<int, Game>();

            gameConsoles.Add(101, new Game("Playstation 4", 101));
            gameConsoles.Add(108, new Game("Xbox 1", 108));
            gameConsoles.Add(110, new Game("PS Vita", 110));
            gameConsoles.Add(104, new Game("Wii U", 104));
            gameConsoles.Add(102, new Game("3DS", 102));

            foreach (KeyValuePair<int, Game> item in gameConsoles)
            {
                Console.WriteLine(item.Value.ToString());
            }

            Console.ReadKey();
        }
    }

    public class Game
    {
        public Game(string name, int id)
        {
            Id = id;
            Name = name;
        }

        public int Id { get; set; }
        public string Name { get; set; }

        public override string ToString()
        {
            return string.Format("Id: {0} Name: {1}", Id, Name);
        }
    }
}

There are three common ways to order items in .NET: 在.NET中订购商品有三种常用方法:

  • Using LINQ (your comments suggest that this is not allowed for the purposes of your assignment) 使用LINQ(您的意见表明,您的作业不允许这样做)
  • Implementing IComparable<Games> in your Games class, or 在您的Games类中实现IComparable<Games> ,或
  • Providing an instance of IComparer<Games> to the Sort method. Sort方法提供IComparer<Games>的实例。

Here is how you implement IComparable<Games> : 以下是如何实现IComparable<Games>

class Games : IComparable<Games> {
    public int CompareTo(Games other) {
        return GameId.CompareTo(other.GameId);
    }
}

Now your Games would sort based on GameId . 现在你的Games将根据GameId排序。

Here is how you use an external IComparer<Games> : 以下是使用外部IComparer<Games>

class CompareGamesOnId : IComparer<Games> {
    int Compare(Games a, Games b) {
        return a.GameId.CompareTo(b.GameId);
    }
}

You call Sort like this: 你打电话给这样Sort

Array.Sort(games, new CompareGamesOnId());

You could use a 1 liner for this. 你可以使用1个衬垫。 LINQ is good for operations which do not affect the set. LINQ适用于不影响集合的操作。

gameConsoles.OrderBy(gc => gc.GameID).ToList().ForEach(g => g.display());

Implement IComparable on Games and use the .Sort method? 在Games上实现IComparable并使用.Sort方法? IComparable IComparable的

You can try LINQ without using the dreaded => symbol, although of course, it is not recommended for simplicity purposes. 您可以在不使用dreaded =>符号的情况下尝试LINQ,当然,出于简化目的,不建议使用它。 However, this explains what exactly is => doing, 但是,这解释了究竟是什么=>做,

foreach (var item in gameConsole.OrderBy(delegate(Game g) { return g.GameId; }))
{
    item.display();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM