1

I have a List of objects(for example, Student(Name: String, RollNo: Int)) and is there a way to sort them in Scala?

Thanks in advance.

2 Answers 2

3

You can use sortWith() function or Ordered trait to sort the List objects. check out the below link:

Sorting a List of custom objects using sortWith function in Scala

Sign up to request clarification or add additional context in comments.

Comments

1

You can do this:

case class Student(name: String, rollNo: Int)

val list = List(Student("bbb",1), Student("aaa",1))

val sortedList = list.sortBy(_.name)

or choose the rollNo property.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.