Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1084597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:35:00+00:00 2026-05-16T22:35:00+00:00

I have a record in erlang: -record(myrec, { id = 0, price = 0,

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:35:01+00:00Added an answer on May 16, 2026 at 10:35 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a record set that includes a date field, and want to determine
Let's say that I have a record in the database and that both admin
I have 3000+ record in my database and I want to Display that record
I have a record store of items which have (name, quantity, owner, status) Now
I have a record store which holds a list of shopping items i am
I have an audit record table that I am writing to. I am connecting
I have a record of data with unix time date in it i want
I have a record returned from MySQL that has a datetime field. What I
I want to have a record editor in PHP and I want to be
I have a record that looks similar to: type TNote = record Title :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.