2

I am having following 2 lists in scala.

    case class Parents(name: String, savings: Double)
    case class Children(parentName: String, debt: Double)

    val parentList:List[Parents] = List(Parents("Halls",1007D), Parents("Atticus",8000D), Parents("Aurilius",900D))

    val childrenList:List[Children] = List(Children("Halls",9379.40D), Children("Atticus",9.48D), Children("Aurilius",1100.75D))

    val sortedParentList:List[Parents] = parentList.sortBy(_.savings).reverse

// sortedParentList = List(Parents(Atticus,8000.0), Parents(Halls,1007.0), Parents(Aurilius,900.0))

now my parenList is Sorted By savings in decreasing order, I want my childrenList to be sorted in the way that it follows parentList Order. i.e. expected order will be following

// sortedParentList = List(Children(Atticus,9.48D), Children(Halls,9379.40D), Children(Aurilius,1100.75D))
3
  • I would suggest to have a Map for Children instead of trying to propagate external sort order to another List Commented Jul 11, 2017 at 9:27
  • Or better, have a List[(Parents, Children)] and sort everything at once - you'll be able to call .unzip on such a list to get separate lists Commented Jul 11, 2017 at 9:27
  • I agree with @Sergey. Rather have a link from parent to child(ren). The fact that you have parents and children does indicate that there should be some association between them. Commented Jul 11, 2017 at 9:30

2 Answers 2

6

Well, if you know both lists are initially in the same order (you can always ensure that by sorting both by name), you can just sort them both in one go:

 val (sortedParentList, sortedChildrenList) = (parents zip children)
   .sortBy(-_._1.savings)
   .unzip

Or you can define the ordering ahead of time, and use it to sort both lists:

val order = parentList.map(p => p.name -> -p.savings).toMap
val sortedParentList = parentList.sortBy(order(_.name))
val sortedChildrenList = childrenList.sortBy(order(_.parentName))

Or you can sort parents first (maybe, they are already sorted), and then define the order:

val order = sortedParentList.zipWithIndex.map { case(p, idx) => p.name -> idx }.toMap
val sortedChildrenList = childrenList.sortBy(c => order(c.parentName))
Sign up to request clarification or add additional context in comments.

3 Comments

I am trying last one, its giving me error error: missing parameter type for expanded function ((x$1) => x$1.parentName)
I am using last one because, I do not want to disturb the order of the parentList. and I want to use that order at many subsequent places
Ok, it didn't like my wildcard ... I fixed the snippet above. Try it now.
1
case class Parents(name: String, savings: Double)
case class Children(parentName: String, debt: Double)

val familiesList: List[(Parents, Children)] = List(
  Parents("Halls",1007D) -> Children("Halls",9379.40D),
  Parents("Atticus",8000D) -> Children("Atticus",9.48D),
  Parents("Aurilius",900D) -> Children("Aurilius",1100.75D))

val (sortedParents, sortedChildren) = familiesList.sortBy {
  case (parents, _) => -parents.savings
}.unzip

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.