2012-12-11 32 views
1

嗨,我有這些記錄我如何能在sort_by方法塊中找到唯一的記錄找到uniq的記錄我怎麼能在sort_by軌一些屬性的基礎

my array is like this 

    [#<Post id: 1, name: "ali", team_elo: 0, team_name: "solo", created_at: 
    "2012-12-09 16:11:17", updated_at: "2012-12-11 03:39:43">, 
    #<Post id: 2, name: "ramiz", team_elo: nil, team_name: "roko", created_at: 
    "2012-12-11 03:40:31", updated_at: "2012-12-11 03:40:31">, 
    #<Post id: 3, name: "ramizrt", team_elo: nil, team_name: "joko", created_at: 
     "2012-12-11 03:40:47", updated_at: "2012-12-11 03:40:47">, 
    #<Post id: 4, name: "lee", team_elo: nil, team_name: "roko", created_at: 
     "2012-12-11 03:41:15", updated_at: "2012-12-11 03:41:15">] 

我怎麼可以排序的唯一TEAM_NAME基地這個數組。結果應該喜歡這個

 [#<Post id: 1, name: "ali", team_elo: 0, team_name: "solo", created_at: 
    "2012-12-09 16:11:17", updated_at: "2012-12-11 03:39:43">, 
    #<Post id: 2, name: "ramiz", team_elo: nil, team_name: "roko", created_at: 
    "2012-12-11 03:40:31", updated_at: "2012-12-11 03:40:31">, 
    #<Post id: 3, name: "ramizrt", team_elo: nil, team_name: "joko", created_at: 
     "2012-12-11 03:40:47", updated_at: "2012-12-11 03:40:47">] 

感謝

回答

0

你可以這樣做:

arr = arr.uniq { |x| x.team_name } 

或其兄弟

arr.uniq! { |x| x.team_name } 

將設置arr只有你想要的元素。

+0

它也將排序此以及 – Kashiftufail

+0

的sort_by添加到末尾:'ARR = arr.uniq(&:TEAM_NAME).sort_by(&:TEAM_NAME)' 請記住,這將通過陣列運行兩次,雖然。 – Zach