Groovy custom sort a map by value

后端 未结 4 809
说谎
说谎 2020-12-31 02:52

I have a map such as

m=[
     \"james\":\"silly boy\",
     \"janny\":\"Crazy girl\",
     \"jimmy\":\"funny man\",
     \"georges\":\"massive fella\"
];
         


        
4条回答
  •  佛祖请我去吃肉
    2020-12-31 03:10

    BTW, here is code which is showing different sorting with and without toLowerCase():

    Map m =[ james  :"silly boy",
             janny  :"crazy girl",
             jimmy  :"Funny man",
             georges:"massive fella" ]
    Map sorted = m.sort { a, b -> a.value <=> b.value }
    println sorted
    sorted = m.sort { a, b -> a.value.toLowerCase() <=> b.value.toLowerCase() }
    println sorted
    

    And wll print:

    [jimmy:Funny man, janny:crazy girl, georges:massive fella, james:silly boy]
    [janny:crazy girl, jimmy:Funny man, georges:massive fella, james:silly boy]
    

提交回复
热议问题