daily pastebin goal
10%
SHARE
TWEET

Untitled

a guest Jan 24th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ary = [[1, 6, 7], [1, 4, 9], [1, 8, 3]]
  2.    
  3. ary2 = ary.sort_by { |a, b, c| b.odd? ? b : c }
  4.    
  5. arr2 = ary.sort_by { |a, b, c| a.odd? ? a : b.odd? ? b : c }
  6.    
  7. ary2 = ary.sort_by do |a, b, c|
  8.   if a.odd?
  9.     a
  10.   elsif b.odd?
  11.     b
  12.   else
  13.     c
  14.   end
  15. end
  16.    
  17. arr2 = arr.sort_by do |a, b, c|
  18.   if a.odd?
  19.     if b.odd?
  20.       if c.odd?
  21.         [a, b, c]
  22.       else
  23.         [a, b]
  24.       end
  25.     elsif c.odd?
  26.       [a, c]
  27.     else
  28.       a
  29.     end
  30.   elsif b.odd?
  31.     if c.odd?
  32.       [b, c]
  33.     else
  34.       b
  35.     end
  36.   else
  37.     c
  38.   end
  39. end
  40.    
  41. arr2 = arr.sort_by do |sub_arr|
  42.   temp = sub_arr.select do |e|
  43.     e.odd?
  44.   end
  45.   temp.empty? ? Array(sub_arr.last) : temp
  46. end
  47.    
  48. def my_sort(arr)
  49.   arr.sort_by {|a| a.any?(&:odd?) ? a.map {|e| e.odd? ? e : Float::INFINITY} : [a.last]}
  50. end
  51.  
  52. my_sort [[1, 6, 7], [1, 4, 9], [1, 2, 3]]
  53.   #=>   [[1, ∞, 7], [1, ∞, 9], [1, ∞, 3]] (sort_by)
  54.   #=>   [[1, 2, 3], [1, 6, 7], [1, 4, 9]]
  55. my_sort [[3, 6, 7], [4, 1, 9], [5, 8, 1]]
  56.   #=>   [[3, ∞, 7], [∞, 1, 9], [5, ∞, 1]] (sort_by)
  57.   #=>   [[3, 6, 7], [5, 8, 1], [4, 1, 9]]
  58. my_sort [[2, 6, 8], [4, 1, 4], [8, 6, 2]]
  59.   #=>   [[8],       [∞, 1, ∞], [2]]       (sort_by)
  60.   #=>   [[8, 6, 2], [2, 6, 8], [4, 1, 4]]
  61. my_sort [[8, 6, 2], [5, 1, 1], [6, 8, 4]]
  62.   #=>   [[2],       [5, 1, 1], [4]        (sort_by)
  63.   #=>   [[8, 6, 2], [6, 8, 4], [5, 1, 1]]
  64.    
  65. ary.sort_by{|a| a.select(&:odd?) | [a.last]}
  66. # => [[1, 8, 3], [1, 6, 7], [1, 4, 9]]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top