在列表中排序Erlang记录? [英] Sorting Erlang records in a list?

查看:133
本文介绍了在列表中排序Erlang记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  -record(myrec,
{
id = 0,
price = 0,
quantity = 0
})。

然后,我需要按ID和价格排序的记录列表,均以降序和升序,其中价格是第一个键,如果两个记录的价格相同,我想按照id排序。



我如何定义一个有趣的? / p>

我是Erlang的新手:)



谢谢,
nisbus

解决方案

这是比迄今为止建议的更短的解决方案。首先定义你的记录:

  1> rd(myrec,{id = 0,price = 0,quantity = 0})。 
myrec

然后让我们发明其中3个:

  2> A = #myrec {id = 1,price = 10,quantity = 2},B = #myrec {id = 2,price = 4,quantity = 3},C = #myrec {id = 3,price = = 1}。 
#myrec {id = 3,price = 10,quantity = 1

现在我们需要比较功能。这是解决方案较短的地方。 Erlang可以按照他们出现的顺序来比较一个元组的条件,所以如果我们要按价格排序,那么通过id,我们只需要比较两个元组,这些元组的形式是 {PriceA,IdA} {PriceB,IdB}

  3> F = fun(X,Y)→> {X#myrec.price,X#myrec.id}< {Y#myrec.price,Y#myrec.id} end。 
#Fun< erl_eval.12.113037538>

并将其插入到列表中:sort / 2

  4>列表:sort(F,[C,B,A])。 
[#myrec {id = 2,price = 4,quantity = 3},
#myrec {id = 1,price = 10,quantity = 2},
#myrec {id = 3,price = 10,quantity = 1}]

订单现在是 [B,A,C] ,您的列表被排序。



请注意,如果您想按降序排序 id,你可以通过颠倒元组中的ids来欺骗它,如下所示:

  5> G = fun(X,Y)→> {X#myrec.price,Y#myrec.id}< {Y#myrec.price,X#myrec.id} end。 
#Fun< erl_eval.12.113037538>
6>列表:sort(G,[C,B,A])。
[#myrec {id = 2,price = 4,quantity = 3},
#myrec {id = 3,price = 10,quantity = 1},
#myrec {id = 1,price = 10,quantity = 2}]

给我们 [B ,C,A] 。这对读者来说不是很明显,所以在这种情况下,您最好记录或使用Dustin的解决方案。这里提出的解决方案的优点是没有需要嵌套。通过在比较中设置元组中的元素,您可以根据需要进行比较,而不必使代码更长。


I have a record in erlang:

-record(myrec,
    { 
      id = 0,
      price = 0,
      quantity = 0
    }).

I then have a list of records that I want to sort by id and price, both in descending and ascending order, where price is the first key and if two records have the same price I want to sort those by id.

How can I define a fun for this?

I'm a newb at Erlang :)

thanks, nisbus

解决方案

This is a shorter solution than what has been suggested so far. First define your record:

1> rd(myrec, {id=0, price=0, quantity=0}).
myrec

Then let's invent 3 of them:

2> A = #myrec{id=1, price=10, quantity=2}, B = #myrec{id=2, price=4, quantity=3}, C = #myrec{id=3, price=10, quantity=1}.
#myrec{id = 3,price = 10,quantity = 1

Now we need a comparison function. This is where the solution is shorter. Erlang can compare terms of a tuple in the order they appear, so if we want to sort by price, then by id, we just have to compare two tuples of the form {PriceA, IdA} < {PriceB, IdB}:

3> F = fun(X, Y) -> {X#myrec.price, X#myrec.id} < {Y#myrec.price, Y#myrec.id} end.
#Fun<erl_eval.12.113037538>

And plug it in lists:sort/2:

4> lists:sort(F, [C,B,A]).
[#myrec{id = 2,price = 4,quantity = 3},
 #myrec{id = 1,price = 10,quantity = 2},
 #myrec{id = 3,price = 10,quantity = 1}]

The order is now [B, A, C] and your list is sorted.

Note that if you wanted to sort by descending id instead, You could trick it by reversing the ids in the tuples as follows:

5> G = fun(X, Y) -> {X#myrec.price, Y#myrec.id} < {Y#myrec.price, X#myrec.id} end.
#Fun<erl_eval.12.113037538>
6> lists:sort(G, [C,B,A]).                                                       
[#myrec{id = 2,price = 4,quantity = 3},
 #myrec{id = 3,price = 10,quantity = 1},
 #myrec{id = 1,price = 10,quantity = 2}]

Giving us [B, C, A]. This is not obvious to the reader, so you'd better document it or use Dustin's solution in this case. The advantage of the solution presented here is that there is no nesting required. By setting elements in either tuple in the comparison, you can pretty much compare as many of them as you want without making the code that much longer.

这篇关于在列表中排序Erlang记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆