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

时间:2016-05-16 08:01:56

标签: java scala playframework scala-collections

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

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

我想根据map的键值对这个arraybuffer进行排序。

我的数组缓冲区看起来像

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”的值对这个arraybuffer进行排序。现在它是字符串,但在排序中我们可以将它改为双倍。

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

将返回已排序的相同或新的arraybuffer。

1 个答案:

答案 0 :(得分:1)

正如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))

希望它有所帮助。