如何在ETS订购商品?

时间:2011-05-30 09:35:36

标签: list erlang

我在ETS中插入了很多项目,如下所示:

ets:insert{{users, jim, 9}}
ets:insert{{users, ham, 3}}
ets:insert{{users, why, 2}}
ets:insert{{users, dhh, 3}}
ets:insert{{users, zend, 4}}

那么如何通过项目中的一个数字获取有序列表?

[
   {users, why, 2},
   {users, ham, 3},
   {users, dhh, 3},
   {users, zend, 4},
   {users, jim, 9}
]

1 个答案:

答案 0 :(得分:4)

像这样使用lists:keysort/2

1> Result = [{users, jim, 9},
             {users, ham, 3},
             {users, why, 2},
             {users, dhh, 3},
             {users,zend, 4}].
[{users,jim,9},
 {users,ham,3},
 {users,why,2},
 {users,dhh,3},
 {users,zend,4}]
2> lists:keysort(3, Result).
[{users,why,2},
 {users,ham,3},
 {users,dhh,3},
 {users,zend,4},
 {users,jim,9}]