# ruby syntax def compare_them (obj1, obj2) if obj1[0].nil? if !obj2[0].nil? return obj1[1] <=> obj2[0] else if obj2[1].nil? if obj1[1] == obj2[0] return -1 else return obj1[1] <=> obj2[0] end else return obj1[1] <=> obj2[1] end end else if obj2[0].nil? if obj1[1].nil? if obj1[0] == obj2[1] return 1 else return obj1[0] <=> obj2[1] end else return obj1[1] <=> obj2[1] end else return obj1[0] <=> obj2[0] end end end foo = [[nil, 1], [nil, 3], [nil, 4], [nil, 5], [nil, 6], [nil, 7], [3, nil], [4, nil], [2, nil], [1, 2], [5, nil], [6, 8], [8, nil], [7, 9], [9, 10]] foo.sort { |a1,a2| compare_them(a1, a2) }