用户已经定义了数组中列的顺序。在ruby中对数组进行排序
order = [:col1, :col2, :col3]
由于用户定义的列顺序表的状态已经改变,当前的列清单
cols = [:col1, :col4, :col3, :col5]
在给定的顺序的cols需要排序。在这种情况下,排序的列可能看起来像这两种情况之一。
[:col2, :col3, :col4, :col5]
[:col2, :col3, :col5, :col4]
这是我的代码让它工作。想知道是否有更好的方法。
#get rid of :col2 since it is not present in the cols listing
sanitized_order = order - (order - cols)
sorted_cols = sanitized_order + (cols - sanitized_order)
我对规范有点困惑。你怎么知道:col4和col5不在列表的开头(例如),因为它们不是排序的一部分?或者是“如果他们不在排序中,那么他们到最后”的规则? – 2009-12-23 14:59:47
如果列没有在顺序列表中定义,那么我会要求将它们放在顺序列中提到的列之后。基本上说:低优先级的col4和col5。 – Roger 2009-12-23 15:05:19