如何根据scala中map的键值对Map的ArrayBuffer进行排序

How to sort ArrayBuffer of Map on the basis of key value of map in scala

如果答案很简单,我很抱歉。我是 Scala 的新手。 我有 Map[String, String] 的数组缓冲区。 完整数据类型如下。

mutable.ArrayBuffer[mutable.Map[String, String]]()

我想根据映射的键值对这个数组缓冲区进行排序。

我的数组缓冲区看起来像,

ArrayBuffer(
    Map(youtube -> , script -> , domain -> EverydayMeArabia.com, cpc -> 0.02, is_mobile -> 0, video_width -> 0, article_id -> 423, url -> http://www.everydaymearabia.com/, mobile_device -> Desktop,Android,iPhone, number_of_article -> 1, user_rate -> null, lang -> en, campaign_id -> 45, twitter -> , name -> كيفيّة تحضير بيتزا بريمافيرا الإيطالية الأصيلة, logo -> , video -> 0, play_video -> 0, rate -> null, facebook -> , image -> ------1-size-3jpg.JPG, video_height -> 0, premium -> 0, params -> null),
    Map(youtube -> https://www.youtube.com/user/everydaymearabia, script -> , domain -> EverydayMeArabia.com, cpc -> 0.02, is_mobile -> 0, video_width -> 0, article_id -> 422, url -> http://www.everydaymearabia.com/حياة-الأسرة/نصائح-للأسرة/مقالة/كيف-تتفوقين-في-مقابلة-عمل, mobile_device -> Desktop,Android,iPhone, number_of_article -> 1, user_rate -> null, lang -> en, campaign_id -> 45, twitter -> , name -> كيف تتفوقين في مقابلة عمل؟, logo -> logoeverydaymepng.png, video -> 0, play_video -> 0, rate -> null, facebook -> , image -> -----1-size-3jpg.JPG, video_height -> 0, premium -> 0, params -> null),
    Map(youtube -> , script -> , domain -> alaan.tv, cpc -> 0.01, is_mobile -> 0, video_width -> 0, article_id -> 488, url -> http://www.alaan.tv/news/technology/130964/15-most-important-job-technology-pay, mobile_device -> Desktop,Android,iPhone, number_of_article -> 2, user_rate -> null, lang -> en, campaign_id -> 48, twitter -> , name -> 15 وظيفة تجعل من أصحابها مليونيرات في عام واحد!, logo -> , video -> 0, play_video -> 0, rate -> null, facebook -> , image -> 15jobsjpg.jpg, video_height -> 0, premium -> 0, params -> null),
    Map(youtube -> , script -> , domain -> alaan.tv, cpc -> 0.01, is_mobile -> 0, video_width -> 0, article_id -> 487, url -> http://www.alaan.tv/news/entertainment/130459/facts-reveals-serious-life-line-discovered, mobile_device -> Desktop,Android,iPhone, number_of_article -> 2, user_rate -> null, lang -> en, campaign_id -> 48, twitter -> , name -> حقائق خطيرة يكشفها خط الحياة عنكم .. اكتشفوها, logo -> , video -> 0, play_video -> 0, rate -> null, facebook -> , image -> handlinesjpg.jpg, video_height -> 0, premium -> 0, params -> null),
    Map(youtube -> null, script -> , domain -> alaan.tv, cpc -> 0.01, is_mobile -> 0, video_width -> 0, article_id -> 483, url -> http://www.alaan.tv/womens-world/entertainment/128348/countries-worlds-most-beautiful-women, mobile_device -> Desktop,Android,iPhone, number_of_article -> 2, user_rate -> null, lang -> en, campaign_id -> 48, twitter -> null, name -> أجمل فتيات العالم في هذه البلاد, logo -> , video -> 0, play_video -> 0, rate -> null, facebook -> null, image -> beautiful-womenjpg.jpg, video_height -> 0, premium -> 0, params -> null)
)

所以想根据键 "cpc" 的值对该数组缓冲区进行排序。现在它是字符串,但在排序中我们可以将其更改为双精度。

cpc -> 0.01 <<<- sort by this value of each map.

这将 return 排序相同或新的数组缓冲区。

正如 Mifeet 提到的:

myArrayBuffer.sortBy(m => m.get("cpc")

应该适合你。

此页面更好地演示了它的工作原理。 Examples of methods available to Scala sequences

会有这个例子:

case class Person(firstName: String, lastName: String)

val fred = Person("Fred", "Flintstone")
val wilma = Person("Wilma", "Flintstone")
val barney = Person("Barney", "Rubble")
val betty = Person("Betty", "Rubble")

val people = List(betty, wilma, barney, fred)

鉴于该数据,这里有一个 sortBy 示例:

people.sortBy(n => (n.lastName, n.firstName))

以下是 Scala REPL 中的几个示例:

scala> people.sortBy(n => (n.lastName, n.firstName))
res1: List[Person] = List(Person(Fred,Flintstone), Person(Wilma,Flintstone), Person(Barney,Rubble), Person(Betty,Rubble))

scala> people.sortBy(n => (n.firstName, n.lastName))
res2: List[Person] = List(Person(Barney,Rubble), Person(Betty,Rubble), Person(Fred,Flintstone), Person(Wilma,Flintstone))

希望对您有所帮助。