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

  • Home
  • SEARCH
  • 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 772111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:45:34+00:00 2026-05-14T18:45:34+00:00

How can it be done in most simply way to write (or maybe there

  • 0

How can it be done in most simply way to write (or maybe there is something embedded in haskell) function which takse as arguments list of tuples (String, Int) and Int x and return top x tuples as list according to x value.

I wonder if its possible to write a function which also takes 3 argument which is the name of (or index) of filed in tuple according to which sorting has to be done.

What are best solutions to make it quite generic?

  • 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-14T18:45:35+00:00Added an answer on May 14, 2026 at 6:45 pm
    take x $ sortBy (compare `on` fst) [("asd", 1), ...]
    

    take x takes the first x items from the sorted list. sortBy sorts the list given as second argument using the sorting function given as the first argument. (compare `on` fst) compares the first values of each tuple.
    Note that this example compares the first value of each tuple for sorting. To sort by the second value, replace fst with snd.

    You see that the sortBy function is very generic, as it lets you define the function used to compare the values. The function takes two arguments and should return one of LT, EQ or GT. Note that the function compare requires both arguments to derive from Ord. The helper function on can be found in the module Data.Function. The function sortBy is in the module Data.List.

    EDIT:
    Here is a complete working example that sorts a list of tuples by comparing their first values and prints the first 2 tuples of the resulting list. Note that I replaced the on from the example above with a equivalent function that shows what on does internally.

    import Data.Function
    import Data.List
    
    main = print $ mySort [("foo", 1), ("bar", 2), ("baz", 3), ("quux", 4)] 2
    
    mySort list x = take x $ sortBy (\ x y -> compare (fst x) (fst y)) list
    

    EDIT:
    As Tom Lokhorst pointed out in his comment, the function comparing from the module Data.Ord is a more readable replacement/shortcut for on compare, so the above could also be written as sortBy (comparing fst).

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

Sidebar

Related Questions

I doubt it can be done portably, but are there any solutions out there?
Can it be done or the only way is to configure it on IIS?
What can be done to resolve the exception: CrystalDecisions.CrystalReports.Engine.InternalException: Failed to open a rowset.
I think it can be done by applying the transformation matrix of the scenegraph
I don't think this can be done cleanly, but I'll ask anyway. I have
Why are pipes considered dangerous to use? What can be done to avoid these
Can this be done by setting a property? I'd prefer that approach then to
Can this be done w/ linqtosql? SELECT City, SUM(DATEDIFF(minute,StartDate,Completed)) AS Downtime FROM Incidents GROUP
Has anyone seen this before - and can anything be done about it? This
What is currying? How can currying be done in C++? Please Explain binders in

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.