I have an array of user records, and I would like to sort the users based on priority, which is stored in an array of strings and this array is always changing.
我有一个用户记录数组,我想根据优先级对用户进行排序,它存储在一个字符串数组中,这个数组总是在变化。
so if i have users =
如果我有用户=
User id: 1, username: "so_admin", priority: "Monday"
User id: 2, username: "so_staff", priority: "Wednesday"
priority = ["Wednesday","Tuesday","Friday"]
优先级=[“星期三”、“星期二”,“星期五”)
How can I sort my users so that users where their priority is "Wednesday" are listed first, etc.
我如何对我的用户进行排序,使他们的优先级为“星期三”的用户名列第一,等等。
1 个解决方案
#1
4
You can do as
你能做的
users.sort_by { |u| priority.index(u.priority) || priority.size }
The above sorting is done, with the assumption that the below Array
will be sorted as per your need, will hold all uniq values. users
array then will use the index of the sorted array.
上面的排序已经完成,假设下面的数组按照您的需要排序,将保留所有uniq值。然后,用户数组将使用排序数组的索引。
priority = ["Wednesday","Tuesday","Friday"]
指数(obj)→int或零
Returns the index of the first object in
ary
such that the object is==
toobj
. Returnsnil
if no match is found.返回位于ary中的第一个对象的索引,以便对象== to obj。如果没有找到匹配项,返回nil。
priority
array doesn't hold all weekdays, rather 3
. I thought, if any users has priority, which is not present in the priority
array, let those users be placed in the last array. Suppose, for any user there is a priority, "Sunday"
, then, that user will be given lowest priority. How ?
优先级数组不是所有工作日,而是3。我想,如果任何用户都有优先级(不在优先级数组中),那么就让这些用户放在最后一个数组中。假设,对于任何有优先级的用户,“Sunday”,那么该用户将被赋予最低优先级。如何?
Look at the expression priority.index(u.priority) || priority.size
, now with above mentioned sample, priority.index("sunday")
gives nil
, and right hand side expression of the ||
will be evaluated, i.e. priority.size
, which 3
. That's how that user
will be moved to the tail of the array.
查看表达式priority.index(u.priority) ||优先级。size,现在有了上面提到的样本,priority.index(“sunday”)为nil, ||的右侧表达式将被求值,即优先级。大小,3。这就是将用户移动到数组尾部的方式。
#1
4
You can do as
你能做的
users.sort_by { |u| priority.index(u.priority) || priority.size }
The above sorting is done, with the assumption that the below Array
will be sorted as per your need, will hold all uniq values. users
array then will use the index of the sorted array.
上面的排序已经完成,假设下面的数组按照您的需要排序,将保留所有uniq值。然后,用户数组将使用排序数组的索引。
priority = ["Wednesday","Tuesday","Friday"]
指数(obj)→int或零
Returns the index of the first object in
ary
such that the object is==
toobj
. Returnsnil
if no match is found.返回位于ary中的第一个对象的索引,以便对象== to obj。如果没有找到匹配项,返回nil。
priority
array doesn't hold all weekdays, rather 3
. I thought, if any users has priority, which is not present in the priority
array, let those users be placed in the last array. Suppose, for any user there is a priority, "Sunday"
, then, that user will be given lowest priority. How ?
优先级数组不是所有工作日,而是3。我想,如果任何用户都有优先级(不在优先级数组中),那么就让这些用户放在最后一个数组中。假设,对于任何有优先级的用户,“Sunday”,那么该用户将被赋予最低优先级。如何?
Look at the expression priority.index(u.priority) || priority.size
, now with above mentioned sample, priority.index("sunday")
gives nil
, and right hand side expression of the ||
will be evaluated, i.e. priority.size
, which 3
. That's how that user
will be moved to the tail of the array.
查看表达式priority.index(u.priority) ||优先级。size,现在有了上面提到的样本,priority.index(“sunday”)为nil, ||的右侧表达式将被求值,即优先级。大小,3。这就是将用户移动到数组尾部的方式。