按第一次出现划分的Scala组

按第一次出现划分的Scala组,scala,group-by,Scala,Group By,我有以下数据: List(Map("weight"->"1000","type"->"abc","match"->"first"),Ma‌​p("weight"->"1000","‌​type"->"abc","match"‌​->"third"),Map("weig‌​ht"->"182","type"->"‌​bcd","match"->"secon‌​d"),Map("weight"->"1‌​50","type"->"bc

我有以下数据:

List(Map("weight"->"1000","type"->"abc","match"->"first"),Ma‌​p("weight"->"1000","‌​type"->"abc","match"‌​->"third"),Map("weig‌​ht"->"182","type"->"‌​bcd","match"->"secon‌​d"),Map("weight"->"1‌​50","type"->"bcd","m‌​atch"->"fourth"),Map‌​("weight"->"40","typ‌​e"->"aaa","match"->"‌​fifth"))
在按“类型”分组后,我希望得到第一个“abc”的结果,然后是“bcd”,然后是“aaa”

当我应用group by“type”时,结果映射给出的第一个键是aaa,而我希望第一个键是“abc”和所有相应的值


如何实现这一点?

如评论中所述,您需要一个排序地图,它不是由简单的分组创建的。你能做的是:

val a = List(Map("weight"->"1000", "type"->"abc","match"->"first"), Map("weight"->"1000","type"->"abc","match"->"third"),Map("weig‌​ht"->"182","type"->"‌​bcd","match"->"secon‌​d"), Map("weight"->"1‌​50","type"->"bcd","m‌​atch"->"fourth"), Map("weight"->"40","type"->"aaa","match"->"‌​fifth"))
val sortedGrouping = SortedMap.empty[String,String] ++ a.groupBy { x => x("type") }
println(sortedGrouping)
您得到(打印)的是:


我不认为有现成的解决方案,你正在努力实现
Map
有两种使用方式:通过键获取值,然后对其进行迭代。第一部分不受排序顺序的影响,因此
groupBy
工作得非常好。第二部分可以通过按所需顺序创建键列表,然后使用该列表以特定顺序从
Map
中获取键值对来实现。例如:

val xs = List(Map("weight"->"1000","type"->"abc","match"->"first"),
              Map("weight"->"1000","type"->"abc","match"->"third"),
              Map("weight"->"182","type"->"bcd","match"->"second"),
              Map("weight"->"150","type"->"bcd","match"->"fourth"),
              Map("weight"->"40","type"->"aaa","match"->"‌fifth"))

import collection.mutable.{ListBuffer, Set}

// List of types in the order of appearance in your list 
val sortedKeys = xs.map(_("type")).distinct

//> sortedKeys: List[String] = List(abc, bcd, aaa)
现在,当您需要迭代时,只需执行以下操作:

val grouped = xs.groupBy(_("type"))

sortedKeys.map(k => (k, grouped(k)))
//  List[(String, List[scala.collection.immutable.Map[String,String]])] =
//  List(
//    (abc,List(Map(weight -> 1000, type -> abc, match -> first),
//              Map(weight -> 1000, type -> abc, match -> third))),
//    (bcd,List(Map(weight -> 182, type -> bcd, match -> second),
//              Map(weight -> 150, type -> bcd, match -> fourth))),
//    (aaa,List(Map(weight -> 40, type -> aaa, match -> ‌fifth)))
//  ) 

请给出应用groupBya.groupBy{x=>x(“type”)}A
Map
的键未排序的代码。如果你想让它们出现,请使用
SortedMap
。好的,对不起,这里应该更清楚。此列表已按值int value排序。所以我认为GroupBy首先会看到第一张地图的出现,并将所有对应的类型都归为“abc”,以此类推。基本上,我希望分组也应该以“价值”为基础。高值组第一个示例:列表(Map(“权重”->“1000”,“类型”->“abc”,“匹配”->“第一”),Map(“权重”->“1000”,“类型”->“abc”,“匹配”->“第三”),Map(“权重”->“182”,“类型”->“bcd”,“匹配”->“第二”),Map(“权重”->“150”,“类型”->“bcd”,“匹配”->“第四”),Map(“权重”->“40”,“类型”->“aaa”,“匹配”->“第五”),按“类型”分组后我想要的结果,首先是“abc”,然后是“bcd”,然后是“aaa”。对不起,我不清楚这个问题。你能再次看到问题中的例子吗?现在我明白了。您不希望对结果进行排序,但保留在列表中找到结果的顺序,对吗?我相信OP希望按键的出现顺序排序。Ruby
Hash
就是这样工作的,只是我不认为Scala有这种现成的解决方案。当然,您不能为此使用标准groupBy。第一部分也可以通过以下方式实现:xs.flatMap{i=>i.get(“type”)}。distinct@Sidhant我同意,不知怎的我错过了。
val grouped = xs.groupBy(_("type"))

sortedKeys.map(k => (k, grouped(k)))
//  List[(String, List[scala.collection.immutable.Map[String,String]])] =
//  List(
//    (abc,List(Map(weight -> 1000, type -> abc, match -> first),
//              Map(weight -> 1000, type -> abc, match -> third))),
//    (bcd,List(Map(weight -> 182, type -> bcd, match -> second),
//              Map(weight -> 150, type -> bcd, match -> fourth))),
//    (aaa,List(Map(weight -> 40, type -> aaa, match -> ‌fifth)))
//  )