2013-11-21 87 views
3

这里是我的对象如何在C#中按升序排序这些对象数组?

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(); 
} 

它基本上会显示每个消息框中的所有5个对象,但我怎么做,以便它可以根据按升序游戏ID订单上显示它们的阵列?

我对排序规则数组进行排序时使用的排序算法。

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; 
      } 
     } 
    } 
} 
+1

linq'OrderBy'? – crashmstr

+0

呃,如果涉及使用“=>”符号,那么我就不会使用它。 – puretppc

+0

为什么不呢?学习一些lambda语法(即'=>')和LINQ(它是一个方法库,如'OrderBy')是非常值得的。或者,你是否需要实施订购算法? –

回答

5

您可以使用使用LINQ OrderBy

foreach(var item in gameConsole.OrderBy(r=> r.GameID)) 
{ 
    item.display(); 
} 
+0

是否可以避免使用=>符号?我不认为我的老师允许我。 – puretppc

+1

@puretppc,它的lambda表达式,您是否必须实现自己的自定义逻辑进行排序? – Habib

+0

我做了一次使用嵌套循环的排序算法。这就是为什么我更喜欢使用对我来说可能是可以理解的代码。我只是不知道如何使用一个对象的数组中的特定属性。 – puretppc

3

有三种常见的订购方式在.NET项目:

这里是你如何实现IComparable<Games>

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

现在你Games将有些基于GameId

这里是你如何使用外部IComparer<Games>

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

你叫Sort这样的:

Array.Sort(games, new CompareGamesOnId()); 
4
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); 
     } 
    } 
} 
2

你可以使用一个1和衬垫这一点。 LINQ适合不影响集合的操作。

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

我希望这么多'ForEach'是'IEnumerable'的扩展... – rae1

+0

@ rae1n - 我经常想知道那个我自己。 –

0

您可以尝试LINQ不使用可怕的=>符号,虽然当然不建议为了简单的目的。然而,这解释了什么=>究竟是做什么,

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