我正在尝试越来越多地了解Clojure,我有以下列表:
{merchant-data {order-id 72144305, unit-price-dollars 110.0, merchant-discount-dollars 20.5, my-discount-dollars 10.2, session-type CONTROL}, my-data {order-id 72144305, unit-price-dollars 110.5, merchant-discount-dollars 10.0, my-discount-dollars 20.2, session-type control}}
{merchant-data {order-id 72144777, unit-price-dollars 200.0, merchant-discount-dollars 30.0, my-discount-dollars 0.0, session-type TEST}, my-data {order-id 72144777, unit-price-dollars 200.0, merchant-discount-dollars 0.0, my-discount-dollars 30.0, session-type test}}
{merchant-data {order-id 72145239, unit-price-dollars 50.0, merchant-discount-dollars 14.8, my-discount-dollars 2.0, session-type UNMANAGED}, my-data {order-id 72145239, unit-price-dollars 50.0, merchant-discount-dollars 5.0, my-discount-dollars 15.0, session-type unmanaged}}
我正在尝试根据特定字段对此列表进行排序(例如,unit-price-dollars):
(排序方式:unit-price-dollars sorted-result)
由于某种原因我没有得到排序列表,请帮忙!如果这不是正确的方法,请建议正确的方法!
答案 0 :(得分:2)
首先,如果您尝试排序的数据结构是有效的Clojure,它总是有帮助的。其次,您需要指定要排序的值的完整路径。
user> (def prices [{:merchant-data {:order-id 72144305, :unit-price-dollars 110.0, :merchant-discount-dollars 20.5, :my-discount-dollars 10.2, :session-type "CONTROL"}, :my-data {:order-id 72144305, :unit-price-dollars 110.5, :merchant-discount-dollars 10.0, :my-discount-dollars 20.2, :session-type "control"}}
{:merchant-data {:order-id 72144777, :unit-price-dollars 200.0, :merchant-discount-dollars 30.0, :my-discount-dollars 0.0, :session-type "TEST"}, :my-data {:order-id 72144777, :unit-price-dollars 200.0, :merchant-discount-dollars 0.0, :my-discount-dollars 30.0, :session-type "test"}}
{:merchant-data {:order-id 72145239, :unit-price-dollars 50.0, :merchant-discount-dollars 14.8, :my-discount-dollars 2.0, :session-type "UNMANAGED"}, :my-data {:order-id 72145239, :unit-price-dollars 50.0, :merchant-discount-dollars 5.0, :my-discount-dollars 15.0, :session-type "unmanaged"}}])
user> (clojure.pprint/pprint (sort-by #(get-in % [:merchant-data :unit-price-dollars]) prices))
({:my-data
{:my-discount-dollars 15.0,
:unit-price-dollars 50.0,
:session-type "unmanaged",
:order-id 72145239,
:merchant-discount-dollars 5.0},
:merchant-data
{:my-discount-dollars 2.0,
:unit-price-dollars 50.0,
:session-type "UNMANAGED",
:order-id 72145239,
:merchant-discount-dollars 14.8}}
{:my-data
{:my-discount-dollars 20.2,
:unit-price-dollars 110.5,
:session-type "control",
:order-id 72144305,
:merchant-discount-dollars 10.0},
:merchant-data
{:my-discount-dollars 10.2,
:unit-price-dollars 110.0,
:session-type "CONTROL",
:order-id 72144305,
:merchant-discount-dollars 20.5}}
{:my-data
{:my-discount-dollars 30.0,
:unit-price-dollars 200.0,
:session-type "test",
:order-id 72144777,
:merchant-discount-dollars 0.0},
:merchant-data
{:my-discount-dollars 0.0,
:unit-price-dollars 200.0,
:session-type "TEST",
:order-id 72144777,
:merchant-discount-dollars 30.0}})
nil
答案 1 :(得分:1)
问题本身会引发问题。例如,上面的地图不包含单位价格美元,但它们的两个子地图确实如此。您想按商家数据,我的数据或两者的某种组合进行排序吗?
尝试更简单的案例,按其中一个值(随机选择的商家数据)进行排序,将所有符号键转换为关键字,并将各个地图放入集合中:
(def test-data
[{:merchant-data {:order-id 72144305, :unit-price-dollars 110.0, :merchant-discount-dollars 20.5, :my-discount-dollars 10.2, :session-type :CONTROL}, :my-data {:order-id 72144305, :unit-price-dollars 110.5, :merchant-discount-dollars 10.0, :my-discount-dollars 20.2, :session-type :control}}
{:merchant-data {:order-id 72144777, :unit-price-dollars 200.0, :merchant-discount-dollars 30.0, :my-discount-dollars 0.0, :session-type :TEST}, :my-data {:order-id 72144777, :unit-price-dollars 200.0, :merchant-discount-dollars 0.0, :my-discount-dollars 30.0, :session-type :test}}
{:merchant-data {:order-id 72145239, :unit-price-dollars 50.0, :merchant-discount-dollars 14.8, :my-discount-dollars 2.0, :session-type :UNMANAGED}, :my-data {:order-id 72145239, :unit-price-dollars 50.0, :merchant-discount-dollars 5.0, :my-discount-dollars 15.0, :session-type :unmanaged}}])
(sort-by (comp :merchant-data :unit-price-dollars) test-data)
; =>
;({:merchant-data {:order-id 72145239, :unit-price-dollars 50.0, :merchant-discount-dollars 14.8, :my-discount-dollars 2.0, :session-type :UNMANAGED}, :my-data {:order-id 72145239, :unit-price-dollars 50.0, :merchant-discount-dollars 5.0, :my-discount-dollars 15.0, :session-type :unmanaged}}
; {:merchant-data {:order-id 72144305, :unit-price-dollars 110.0, :merchant-discount-dollars 20.5, :my-discount-dollars 10.2, :session-type :CONTROL}, :my-data {:order-id 72144305, :unit-price-dollars 110.5, :merchant-discount-dollars 10.0, :my-discount-dollars 20.2, :session-type :control}}
; {:merchant-data {:order-id 72144777, :unit-price-dollars 200.0, :merchant-discount-dollars 30.0, :my-discount-dollars 0.0, :session-type :TEST}, :my-data {:order-id 72144777, :unit-price-dollars 200.0, :merchant-discount-dollars 0.0, :my-discount-dollars 30.0, :session-type :test}})
如果这不是你所追求的,那么你将不得不让你的问题更清楚。