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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:04:33+00:00 2026-05-18T00:04:33+00:00

I need to sort an array of pairs by second element. How do I

  • 0

I need to sort an array of pairs by second element. How do I pass comparator for my pairs to the quickSort function?
I’m using the following ugly approach now:

type AccResult = (AccUnit, Long) // pair
class Comparator(a:AccResult) extends Ordered[AccResult] {
        def compare(that:AccResult) = lessCompare(a, that)
        def lessCompare(a:AccResult, that:AccResult) = if (a._2 == that._2) 0 else if (a._2 < that._2) -1 else 1
}
scala.util.Sorting.quickSort(data)(d => new Comparator(d))

Why is quickSort designed to have an ordered view instead of usual comparator argument?

Scala 2.7 solutions are preferred.

  • 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-18T00:04:33+00:00Added an answer on May 18, 2026 at 12:04 am

    Ok, I’m not sure exactly what you are unhappy about what you are currently doing, but perhaps all you are looking for is this?

    implicit def toComparator(a: AccResult) = new Comparator(a)
    scala.util.Sorting.quickSort(data)
    

    If, on the other hand, the problem is that the tuple is Ordered and you want a different ordering, well, that’s why it changed on Scala 2.8.

    * EDIT *

    Ouch! Sorry, I only now realize you said you preferred Scala 2.7 solutions. I have editted this answer soon to put the solution for 2.7 above. What follows is a 2.8 solution.

    Scala 2.8 expects an Ordering, not an Ordered, which is a context bound, not a view bound. You’d write your code in 2.8 like this:

    type AccResult = (AccUnit, Long) // pair 
    implicit object AccResultOrdering extends Ordering[AccResult] { 
            def compare(x: AccResult, y: AccResult) = if (x._2 == y._2) 0 else if (x._2 < y._2) -1 else 1 
    }
    

    Or maybe just:

    type AccResult = (AccUnit, Long) // pair 
    implicit val AccResultOrdering = Ordering by ((_: AccResult)._2)
    

    And use it like:

    scala.util.Sorting.quickSort(data)
    

    On the other hand, the usual way to do sort in Scala 2.8 is just to call one of the sorting methods on it, such as:

    data.sortBy((_: AccResult)._2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to sort an array of ints using a custom comparator, but Java's
I need to sort tuples according to the second element of each tuple but
Long story short, I need to sort an array of objects using usort, and
I need to sort an array in PHP and then sort by its second
I'm using Ruby 1.8.7. I have the following array of hashes. I need to
I have an array of filenames and need to sort that array by the
To preface I am using Borland C++ and the VCL. I need some sort
I need to sort an array containing arrays in ascending numerical order. The data
I need to sort associative array by JS for one of my projects. I
Need your expert guidance as how to sort a 2D array, row-wise ( means

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.