Landei | 10 Feb 00:28
Picon
Picon

Re: sort on two keys


Mohamed Bana-6 wrote:
> 
> Hi All,
> 
> What would be the equivalent of the C# program below?
> 
> var counts = new Dictionary<string, int>();
> 
> var sortedDict =
>   from entry in counts
>   orderby entry.Value descending, entry.Key ascending
>   select entry;
> 
> that is, sort by max no. then alphabetically.
> 
> —Mohamed
> 
> 

I hope I got it right...

val m = Map("one"->1,"once"->1,"two"->2,"twice"->2,"double"->2,"three"->3)
m.toList.sort((x,y) => if (x._2 == y._2) x._1 < y._1 else x._2 > y._2)
//--> res1: List[(java.lang.String, Int)] = List((three,3), (double,2),
(twice,2), (two,2), (once,1), (one,1))

Cheers,
Daniel

--

-- 
View this message in context: http://old.nabble.com/-scala--sort-on-two-keys-tp27522069p27523939.html
Sent from the Scala mailing list archive at Nabble.com.


Gmane