How do I sort an array in Scala?

后端 未结 7 640
北恋
北恋 2020-12-07 12:54

I can see there\'s a sorting object, Sorting, with a quicksort method, quickSort, on it.

What would be a code example of using it, sorting

相关标签:
7条回答
  • 2020-12-07 13:35

    If you just want to sort things, but aren't married to the Sorting object in particular, you can use the sort method of List. It takes a comparison function as an argument, so you can use it on whatever types you'd like:

    List("Steve", "Tom", "John", "Bob").sort((e1, e2) => (e1 compareTo e2) < 0)
    
    List(1, 4, 3, 2).sort((e1, e2) => (e1 < e2))
    

    Lists probably qualify as "more scalaish" than arrays.

    From the scala api docs:

    def sort(lt : (A, A) => Boolean) : List[A]

    Sort the list according to the comparison function <(e1: a, e2: a) =>
    

    Boolean, which should be true iff e1 is smaller than e2.

    0 讨论(0)
  • 2020-12-07 13:38

    With Scala 2.8 or later it is possible to do:

    List(3,7,5,2).sortWith(_ < _)
    

    that uses java.util.Arrays.sort, an implementation of quicksort.

    0 讨论(0)
  • 2020-12-07 13:49

    Nowadays this one works too:

    List(3,7,5,2).sorted

    0 讨论(0)
  • 2020-12-07 13:50

    Sorting.quickSort declares functions for taking an Array of numbers or Strings, but I'm assuming you mean you want to sort a list of objects of your own classes?

    The function I think you're looking at is

    quickSort [K](a : Array[K])(implicit view$1 : (K) => Ordered[K]) : Unit
    

    Which, if I'm reading this right, means that the objects in the Array must have the Ordered trait. So your class must extend Ordered (or must mix it in), and therefore must implement the compare method of that trait.

    So to rip off an example from the book:

    class MyClass(n: Int) extends Ordered[MyClass] {
       ...
      def compare(that: MyClass) =
        this.n - that.n
    }
    

    So given an Array[MyClass], then Sorting.quickSort should work.

    0 讨论(0)
  • 2020-12-07 13:51
    val array = Array((for(i <- 0 to 10) yield scala.util.Random.nextInt): _*)
    scala.util.Sorting.quickSort(array)
    

    Scala's "default" array is a mutable data structure, very close to Java's Array. Generally speaking, that means an "array" is not very Scala-ish, even as mutable data structures go. It serves a purpose, though. If array is the right data type for your need, then that is how you sort it. There are other sorting methods on object Sorting, by the way.

    I think I just realized what your question is... you don't need to pass any implicit parameter (it's implicit, after all). That parameter exists to say that there must be some way to convert the type K into an Ordered[K]. These definitions already exist for Scala's classes, so you don't need them.

    For an arbitrary class you can define it this way:

    scala> case class Person(name: String)
    defined class Person
    
    scala> val array = Array(Person("John"), Person("Mike"), Person("Abe"))
    array: Array[Person] = Array(Person(John), Person(Mike), Person(Abe))
    
    scala> scala.util.Sorting.quickSort(array)
    <console>:11: error: no implicit argument matching parameter type (Person) => Ordered[Person] was found.
           scala.util.Sorting.quickSort(array)
                                       ^
    scala> class OrderedPerson(val person: Person) extends Ordered[Person] {
         | def compare(that: Person) = person.name.compare(that.name)
         | }
    defined class OrderedPerson
    
    scala> implicit def personToOrdered(p: Person) = new OrderedPerson(p)
    personToOrdered: (p: Person)OrderedPerson
    
    scala> scala.util.Sorting.quickSort(array)
    
    scala> array
    res8: Array[Person] = Array(Person(Abe), Person(John), Person(Mike))
    

    Now, if Person was Ordered to begin with, this wouldn't be a problem:

    scala> case class Person(name: String) extends Ordered[Person] {
         | def compare(that: Person) = name.compare(that.name)
         | }
    defined class Person
    
    scala> val array = Array(Person("John"), Person("Mike"), Person("Abe"))
    array: Array[Person] = Array(Person(John), Person(Mike), Person(Abe))
    
    scala>  scala.util.Sorting.quickSort(array)
    
    scala> array
    res10: Array[Person] = Array(Person(Abe), Person(John), Person(Mike))
    
    0 讨论(0)
  • 2020-12-07 13:52

    I prefer to user Sorting util

    Example :

    val arr = Array(7,5,1, 9,2)
    
    scala.util.Sorting.quickSort(arr)
    

    please read this for more info Sorting util

    0 讨论(0)
提交回复
热议问题