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 7679429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:52:31+00:00 2026-05-31T17:52:31+00:00

I got a custom Data type named Student which has marks for 2 subjects.

  • 0

I got a custom Data type named Student which has marks for 2 subjects. I have created a function named average that calculates the average of two. All work fine.

My question is how can I sort list of students by their average?

data Student = Student
    {studentName :: String,
     subject1 :: Double,
     subject2 :: Double} deriving (Show)

average :: Student -> Double
average (Student _ sub1 sub2) = (sub1 + sub2) / 2

students :: [Student]
students = [Student "Dave"  50.0  40.0,
            Student "Joe"   65.0  90.0,
            Student "Ann"   75.0  82.0]

P.S. I am a beginner in Haskell and don’t know whether its got a
inbuilt average function but I prefer if I can sort my list in similar
way without using inbuilt average function (if it is there) as this
small test solution to be use with a different type of function
instead average.

  • 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-31T17:52:32+00:00Added an answer on May 31, 2026 at 5:52 pm
    import Data.Function (on)
    import Data.List (sortBy)
    
    studentsSortedByAverage = sortBy (compare `on` average) students
    

    Note that those are backticks around on, not single quotes.

    Here are links to docs for sortBy and on.


    If you are using an old compiler that doesn’t come with Data.Function, here is the definition of on:

    on :: (b -> b -> c) -> (a -> b) -> a -> a -> c
    (.*.) `on` f = \x y -> f x .*. f y
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a custom component that has children components dynamically added and removed to
I have a custom Http Handler which manipulates HTTP POST and GET. I got
I've got a custom function, which calls ajax request and I'm able to save
Alright, I have some data that I need to assign an int type identifier
I have a Model that contains a List of a custom type. I want
We have got a custom MembershipProvider in ASP.NET . Now there are 2 possible
I've got a custom ant script for building and other fun stuff. IntelliJ has
I've got a custom javascript autocomplete script that hits the server with multiple asynchronous
I've got a custom attribute that I want to apply to my base abstract
I've got a custom AS3 scrollbar that I need to modify. The scroll thumb

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.